Building an app according to "thinking in react"
Tags: beginners, react, javascript, frontend
Published: 2020-06-02 15:56:42

---
title: Building an app according to "thinking in react"
published: true
description: This is a tutorial of how to use the react docs in order to build a simple app
tags: beginners, react, javascript, frontend
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/ek6t1zob725y14h55s3x.png
---

If we dig in <a href="https://reactjs.org/docs/thinking-in-react.html">React Docs</a> we will find a great article called “thinking in react”. It’s the last chapter of the main concepts, so it's **essential** to go through all the previous chapters before continuing here. In this article we'll build a simple app with react, by following the 5 steps in the chapter.

(the code for this app lives on [github](https://github.com/PabloPap/time-machine))

<p>First things first: we draw a mockup either on paper or using software - there are many out there.</p>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gw02dkfqg8bks7m8eexb.png)

<hr>
## Step 1: Break The UI Into A Component Hierarchy 

We have five components in our app. 

<ol>
  <li><strong>App (green): </strong>it's the top level component, contains everything inside the app</li>
  <li><strong>AddDate (red): </strong>receives the date input from user</li>
  <li><strong>DateList (brown): </strong>displays a list of cards based on the date user input</li>
  <li><strong>Date (blue): </strong>displays a card for each date and receives the task input from user</li>
  <li><strong>Task (orange): </strong>displays the task paragraph</li>
</ol>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2agziwzjqdfziap74f7r.png)

Our components hierarchy is:

- App
    - AddDate
    - DateList
        - Date
            - Task

(components that appear within another component in the mock should appear as a child in the hierarchy)

<hr>
## Step 2: Build A Static Version in React

(<a href="https://github.com/PabloPap/time-machine/tree/static-version">git branch for this step</a>)

<p>Now it's time to add the components so that we get a static layout of our app. In this step no interactivity is involved. As the docs mention, it’s usually easier in simple apps to build our components top-down (in our example starting from App component).</p>

#### App
```react
import React, { Component } from 'react';
import './App.css';
import AddDate from './AddDate';
import DateList from './DateList';

class App extends Component {
  render() {
    const dates = ['2018-04-23', '2019-06-13', '2014-09-29'];
    return (
      <div className="App">
        <header className="App-header">
          <h1>Time Machine</h1>
        </header>
        <AddDate dates={dates} />
        <DateList dates={dates} />
      </div>
    );
  }
}

export default App;
```

#### AddDate
```react
import React, { Component } from 'react';

class AddDate extends Component {
  render() {
    return (
      <div className="App__form">
        <form className="App__form--date">
          <div className="App__form--body">
            <label>Choose Your Past:</label>
            <input type="date" max={new Date().toISOString().split('T')[0]} />
          </div>

          <div className="App__form--btn">
            <button type="submit">Add Date</button>
          </div>
        </form>
      </div>
    );
  }
}

export default AddDate;
```

#### DateList
```react
import React, { Component } from 'react';
import Date from './Date';

class DateList extends Component {
  render() {
    const { dates } = this.props;

    return (
      <div className="App__list">
        <h2 className="App__list--title">Missions</h2>
        <ul className="App__list--items">
          {dates.map((date) => (
            <Date date={date} key={date} />
          ))}
        </ul>
      </div>
    );
  }
}

export default DateList;
```

#### Date
```react
import React, { Component } from 'react';
import Task from './Task';

class Date extends Component {
  render() {
    const { date } = this.props;
    return (
      <li>
        <div className="App__card--inner">
          <h2>{date}</h2>
          <form onSubmit={this.handleFormSubmit} className="App__card">
            <div className="App__card--form">
              <label>Add Your Task</label>
              <textarea
                rows="3"
                cols="30"
                placeholder="type here..."
                required
              ></textarea>
            </div>
            <div className="App__card--btn">
              <button type="submit">Add Task</button>
            </div>
          </form>
          <Task />
        </div>
      </li>
    );
  }
}

export default Date;
```
#### Task
```react
import React from 'react';

const Task = () => {
  return (
    <div className="App__task">
      <h3>Task</h3>
      <p>this is the task paragraph</p>
    </div>
  );
};

export default Task;
```
<hr>
## Step 3: Identify The Minimal (but complete) Representation Of UI State

<p>To add interactivity to our app we must create pieces of state to the data model.</p>

<p>The data in our app are:</p>
<ol>
  <li>The list of dates we pass to DateList component</li>
  <li>The new date we get from user input</li>
  <li>The error message that appears when the user enters a date that already exists</li>
  <li>The error message that appears when the user removes a date selected on input and submits the empty date</li>
  <li>The card-date that is shown as title on the card</li>
  <li>The card-text the user has entered on the task textbox</li>
  <li>The card-task that appears as a paragraph after "add task" submission</li>
</ol>

<p>Now we have to go through three questions to find out with piece of data is considered state</p>

<ol>
  <li>Is it passed in from a parent via props? If so, it probably isn’t state.</li>
  <li>Does it remain unchanged over time? If so, it probably isn’t state.</li>
  <li>Can you compute it based on any other state or props in your component? If so, it isn’t state.</li>
</ol>

The list of dates and the new date we get from user are changing over time and cannot be computed based on any other state or props, therefore will be state.

The error messages are changing over time and we could compute them from 'dates' props and 'date' state inside the render method. However, we want the errors to appear only on submission and not on every page re-rendering, so we treat them as pieces of state.

The card-date changes over time but it can be computed from the 'dates' state so it's not state.

The card-text is state because it's changing over time and cannot be computed based on any other state or props. 

The card-task is changing over time. Although it can be computed from the 'value' state we need to show the text in the paragraph only on user submission, hence we should treat it as state.

Finally, our state is:

<ul>
  <li>The list of dates</li>
  <li>The new date from the user input</li>
  <li>The same date error message</li>
  <li>The empty date error message</li>
  <li>The value of the textbox in the card</li>
  <li>The task that is passed as a paragraph in the card</li>
</ul>
<hr>

## Step 4: Identify Where Your State Should Live

For each piece of state in our application:

<ul>
  <li>Identify every component that renders something based on that state.</li>
  <li>Find a common owner component (a single component above all the components that need the state in the hierarchy).</li>
  <li>Either the common owner or another component higher up in the hierarchy should own the state.</li>
  <li>If we can’t find a component where it makes sense to own the state, create a new component solely for holding the state and add it somewhere in the hierarchy above the common owner component.</li>
</ul>

`dates`:
The DateList component renders the 'dates'. The AddDate Component shows the error message based on whether the 'dates' already include the date inserted by the user. In order for both of these components to access the 'dates' piece of state, we need to move the 'dates' state to their parent - common owner component which is the App component.

`date`:
This piece of state lives in the AddDate Component because that's the component where the user picks a date and we want to control the behaviour of the input.

`dateExists / dateEmpty`:
These pieces of state should live in the AddDate Component because that's the component that will have to show an error message if this date already exists or if the date field is empty.

`value`: 
This piece of state lives in the Date component because that's the component where the user enters the text and we want to control the behavior of this input.

`task`:
This piece of state lives in the Date component because that's the component where we can grab the user's text and pass it down to the Task component.

(<a href="https://github.com/PabloPap/time-machine/tree/state-locations">git branch for this step</a>)

#### App
```React
import React, { Component } from 'react';
import './App.css';
import AddDate from './AddDate';
import DateList from './DateList';

class App extends Component {
  state = {
    dates: [],
  };

  render() {
    const dates = ['2018-04-23', '2019-06-13', '2014-09-29'];
    return (
      <div className="App">
        <header className="App-header">
          <h1>Time Machine</h1>
        </header>
        <AddDate dates={dates} />
        <DateList dates={dates} />
      </div>
    );
  }
}

export default App;
```

#### AddDate
```react
import React, { Component } from 'react';

class AddDate extends Component {
  state = {
    date: new Date().toISOString().split('T')[0],
    dateExists: false,
    dateEmpty: false,
  };

  render() {
    return (
      <div className="App__form">
        <form onSubmit={this.handleFormSubmit} className="App__form--date">
          <div className="App__form--body">
            <label>Choose Your Past:</label>
            <input type="date" max={new Date().toISOString().split('T')[0]} />
          </div>

          <div className="App__form--btn">
            <button type="submit">Add Date</button>
          </div>
        </form>
      </div>
    );
  }
}

export default AddDate;
```

#### DateList
```react
import React, { Component } from 'react';
import Date from './Date';

class DateList extends Component {
  render() {
    const { dates } = this.props;

    return (
      <div className="App__list">
        <h2 className="App__list--title">Missions</h2>
        <ul className="App__list--items">
          {dates.map((date) => (
            <Date date={date} key={date} />
          ))}
        </ul>
      </div>
    );
  }
}

export default DateList;
```

#### Date
```react
import React, { Component } from 'react';
import Task from './Task';

class Date extends Component {
  state = {
    value: '',
    task: '',
  };

  render() {
    const { date } = this.props;

    return (
      <li>
        <div className="App__card--inner">
          <h2>{date}</h2>
          <form onSubmit={this.handleFormSubmit} className="App__card">
            <div className="App__card--form">
              <label>Add Your Task</label>
              <textarea
                rows="3"
                cols="30"
                placeholder="type here..."
                required
              ></textarea>
            </div>
            <div className="App__card--btn">
              <button type="submit">Add Task</button>
            </div>
          </form>
          <Task task={this.state.task} />
        </div>
      </li>
    );
  }
}

export default Date;
```

#### Task
```react
import React from 'react';

const Task = (props) => {
  return (
    <div className="App__task">
      <h3>Task</h3>
      <p>{props.task}</p>
    </div>
  );
};

export default Task;
```
<hr>
## Step 5: Add Inverse Data Flow

In this step we want to access data the other way around: from child to parent component. Components should only update their own state so when a user adds a new date on the AddDate component it cannot have access directly to the dates state inside the App component. The way we can have access is by passing a callback from App to AddDate that will get triggered when the state should be updated. The onAddDate callback will be passed as a prop to AddDate component and when a new date is added, the callback runs and a new date is passed to the App component.

(<a href="https://github.com/PabloPap/time-machine/tree/inverse-data-flow">git branch for this step</a>)

#### App
```React
import React, { Component } from 'react';
import './App.css';
import AddDate from './AddDate';
import DateList from './DateList';

class App extends Component {
  state = {
    dates: [],
  };

  addDate = (date) => {
    this.setState((currState) => ({
      dates: [...currState.dates, date],
    }));
  };

  render() {
    return (
      <div className="App">
        <header className="App-header">
          <h1>Time Machine</h1>
        </header>
        <AddDate dates={this.state.dates} onAddDate={this.addDate} />
        <DateList dates={this.state.dates} />
      </div>
    );
  }
}

export default App;
```
#### AddDate
```react
import React, { Component } from 'react';

class AddDate extends Component {
  state = {
    date: new Date().toISOString().split('T')[0],
    dateExists: false,
    dateEmpty: false,
  };

  sameDateExists = (currDate) => {
    const dates = this.props.dates;
    for (let date of dates) {
      if (date === currDate) {
        return true;
      }
    }
    return false;
  };

  handleFormSubmit = (event) => {
    event.preventDefault();

    const dateExists = this.sameDateExists(this.state.date);

    if (!dateExists && this.state.date) {
      this.props.onAddDate(this.state.date);
      this.setState({ dateEmpty: false });
    }

    if (!this.state.date) {
      this.setState({ dateEmpty: true });
    }

    if (dateExists) {
      this.setState({ dateEmpty: false });
    }

    this.setState({ dateExists });
  };

  handleDateChange = (event) => {
    const { value } = event.target;
    this.setState((currState) => ({
      ...currState,
      date: value,
    }));
  };

  render() {
    return (
      <div className="App__form">
        <form onSubmit={this.handleFormSubmit} className="App__form--date">
          <div className="App__form--body">
            <label>Choose Your Past:</label>
            <input
              type="date"
              max={new Date().toISOString().split('T')[0]}
              onChange={this.handleDateChange}
            />
          </div>

          <div className="App__form--btn">
            <button type="submit">Add Date</button>
          </div>
        </form>
        {this.state.dateExists ? (
          <p className="App__form--error">This date has already been chosen</p>
        ) : (
          ''
        )}
        {this.state.dateEmpty ? (
          <p className="App__form--error">Please choose a date</p>
        ) : (
          ''
        )}
      </div>
    );
  }
}
export default AddDate;
```

#### DateList
```react
import React, { Component } from 'react';
import Date from './Date';

class DateList extends Component {
  render() {
    const { dates } = this.props;
    return (
      <div className="App__list">
        <h2 className="App__list--title">Missions</h2>
        <ul className="App__list--items">
          {dates.map((date) => (
            <Date date={date} key={date} />
          ))}
        </ul>
      </div>
    );
  }
}

export default DateList;
```

#### Date
```react
import React, { Component } from 'react';
import Task from './Task';

class Date extends Component {
  state = {
    value: '',
    task: '',
  };

  handleFormSubmit = (event) => {
    event.preventDefault();

    this.setState({
      task: this.state.value,
    });
  };

  handleAddTask = (event) => {
    this.setState({
      value: event.target.value,
    });
  };

  render() {
    const { date } = this.props;

    return (
      <li>
        <div className="App__card--inner">
          <h2>{date}</h2>
          <form onSubmit={this.handleFormSubmit} className="App__card">
            <div className="App__card--form">
              <label>Add Your Task</label>
              <textarea
                rows="3"
                cols="30"
                placeholder="type here..."
                value={this.state.value}
                onChange={this.handleAddTask}
                required
              ></textarea>
            </div>
            <div className="App__card--btn">
              <button type="submit">Add Task</button>
            </div>
          </form>
          <Task task={this.state.task} />
        </div>
      </li>
    );
  }
}

export default Date;
```
#### Task
```react
import React from 'react';

const Task = (props) => {
  return (
    <div className="App__task">
      <h3>Task</h3>
      <p>{props.task}</p>
    </div>
  );
};

export default Task;
```
<hr>
## Finish line
Now we have a guidance on how we can break our UI into tiny pieces and then create different versions. One static version that simply takes our data model and renders the UI and the final version where interactivity is added.

I hope you had fun following this tutorial on building a react app!

You can find the code for this app [here](https://github.com/PabloPap/time-machine).

The app is also up and running [here](https://time-machine-app.netlify.app/)

Thanks for reading!