Journey, React

Day 5: Let’s build some complex components

**For this section I am going to assume you have installed node with npm and create react app** If you have not  you can follow this tutorial for installing node and this tutorial for installing create-react-app. **

Let’s try to recreate the timeline section of Twitter.

But before we do any coding let’s do some planning and think about how we will go about this.

  1. We know we have two parent components but these should probably go in a separate container and we will call it app
  2. The app container will have two components called the title and content.
  3. For simplicity purposes the content will all be hard coded, for now.

Let’s stat coding

1. Building the Container

Let’s build the main component, and because when we created the new folder with create-react-app it created everything we need with it. We can just clean up the app.js file to look like this:

Screen Shot 2018-05-16 at 3.49.04 PM

This will be the parent component for the whole application. This is code that we will be using a lot. This is probably something that will be second nature as the days go by. 

2. Building the Child Components

We are going to build two separate components. Each component will be a separate file. First thing we want to do is to start putting our components in a separate folder. Let’s call the folder components and each file will have the extension of .js.

The folder structure will look like this

  • App.html
  • component folder
    • Title.js
    • Content.js

Let’s build the title component and it will look like this

Screen Shot 2018-05-16 at 4.01.07 PM

There are a few lines of code that are new. Mainly at the top and bottom.

Let’s talk about these lines.

import React, { Component } from 'react';

To work with React we need to import React and it’s component class. React is the default export of react, so it’s not necessary to put curly braces around it. Component is not a default export so it does need to be wrapped in the curly braces.

export default Title;

For now it’s good enough to know that Export and Import treat the information as a variable that is transferred back and forth between files.

Which means that we need to import the data and the place where we will be doing that is the App.js file.

At the top of the file we will add the line

import Title from './components/Title.js';

This line will take the content that was exported from the file at ‘./components/Title.js’ and put it in the App.js file.

Screen Shot 2018-05-16 at 4.01.51 PM

The content child is a bit more code but it’s not that much different

Screen Shot 2018-05-16 at 4.14.01 PM

It looks like a lot of code because we hard coded the information for our first timeline post.

Because we exported the content we need to import it into our App.js file. If we save everything and start our server there is a white page. Even though we did all this work, we have nothing to show for it.


No reason to be mad yet, we just need to

Put it all together

If we don’t tell React what to render it will not do anything. We imported the files but we need to set the Title and Content components into the App component. We do this with two lines.

Screen Shot 2018-05-16 at 4.26.59 PM

<Title />
<Content />

will do this for us.

And that’s all there is to it. We are now able to write multiple components and we can build more complex applications.

But you might have noticed that it’s lacking something and that something we will work on in future posts. We will be creating some dynamic data.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s