[React MAIN CONCEPTS] 12. Thinking in React

12. Thinking in React

React is, in our opinion, the premier way to build big, fast Web apps with JavaScript. It has scaled very well for us at Facebook and Instagram.

Break The UI Into A Component Hierarchy

But how do you know what should be its own component? Use the same techniques for deciding if you should create a new function or object. One such technique is the single responsibility principle, that is, a component should ideally only do one thing. If it ends up growing, it should be decomposed into smaller subcomponents.

thinking-in-react-components

You’ll see here that we have five components in our app. We’ve italicized the data each component represents.

  • FilterableProductTable (orange): contains the entirety of the example

  • SearchBar (blue): receives all user input

  • ProductTable (green): displays and filters the data collection based on user input

  • ProductCategoryRow (turquoise): displays a heading for each category

  • ProductRow (red): displays a row for each product

Now that we’ve identified the components in our mock, let’s arrange them into a hierarchy. Components that appear within another component in the mock should appear as a child in the hierarchy:

-FilterableProductTable

  • SearchBar

  • ProductTable

    • ProductCategoryRow

    • ProductRow

Identify The Minimal (but complete) Representation Of UI State

To make your UI interactive, you need to be able to trigger changes to your underlying data model. React achieves this with state.

To build your app correctly, you first need to think of the minimal set of mutable state that your app needs. The key here is DRY: Don’t Repeat Yourself. Figure out the absolute minimal representation of the state your application needs and compute everything else you need on-demand. For example, if you’re building a TODO list, keep an array of the TODO items around; don’t keep a separate state variable for the count. Instead, when you want to render the TODO count, take the length of the TODO items array.

Think of all the pieces of data in our example application. We have:

  • The original list of products

  • The search text the user has entered

  • The value of the checkbox

  • The filtered list of products

Let’s go through each one and figure out which one is state. Ask three questions about each piece of data:

  • Is it passed in from a parent via props? If so, it probably isn’t state.

  • Does it remain unchanged over time? If so, it probably isn’t state.

  • Can you compute it based on any other state or props in your component? If so, it isn’t state.

The original list of products is passed in as props, so that’s not state. The search text and the checkbox seem to be state since they change over time and can’t be computed from anything. And finally, the filtered list of products isn’t state because it can be computed by combining the original list of products with the search text and value of the checkbox.

So finally, our state is:

  • The search text the user has entered

  • The value of the checkbox

Identify Where Your State Should Live


Remember: React is all about one-way data flow down the component hierarchy. It may not be immediately clear which component should own what state. This is often the most challenging part for newcomers to understand, so follow these steps to figure it out:

For each piece of state in your application:

  • Identify every component that renders something based on that state.

  • Find a common owner component (a single component above all the components that need the state in the hierarchy).

  • Either the common owner or another component higher up in the hierarchy should own the state.

  • If you 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.


Add Inverse Data Flow

Let’s think about what we want to happen. We want to make sure that whenever the user changes the form, we update the state to reflect the user input. Since components should only update their own state, FilterableProductTable will pass callbacks to SearchBar that will fire whenever the state should be updated. We can use the onChange event on the inputs to be notified of it. The callbacks passed by FilterableProductTable will call setState(), and the app will be updated.

References

[1] Thinking in React – React - https://reactjs.org/docs/thinking-in-react.html

[2] React – A JavaScript library for building user interfaces - https://reactjs.org/