Cheat sheet to common React terms
Tags: react, cheatsheet, frontend, javascript
Published: 2022-01-30 16:00:35

React.js is a Javsacript library that is created by Facebook. The benefits of using react is enabling code reusability, faster development time and higher web performance. If you remember the days of implementing AJAX for page components update, you will find that react makes creating dynamic UI a breeze.

The popularity of React among developers and enterprise as the top frontend framework remains almost unchallenged by other popular frontend framework like angular and vue.

<img src="https://res.cloudinary.com/thegeekwing/image/upload/v1584773446/blog/2020/03/reactjs/npm-frontend-trend.jpg">

If you are just starting to venture into frontend web development, React is a good place to look onto of your Javascript toolbox. Here is a quick cheat sheet to introduce you to the world of React.

## Components

To understand any React codebase, you need to think in terms of components. Think of the web as many rectangles. Even for irregular shapes, you can draw rectangles around them. These rectangles might overlap each other, have a certain padding or margin around them, but they are all certainly derive from a rectangle.

If you want to see the components in actions, the React documentation has a great breakdown on components-based thinking. Next, we will look at the two common types of React components.

<blockquote>
<div class="d-flex flex-column flex-md-row justify-content-md-between">

<div>
<strong>Read</strong>: <a href="https://reactjs.org/docs/thinking-in-react.html" target="_blank">Thinking in React</a>
</div>

<div>-- <em>React documentation</em></div>

</div>
</blockquote>

## Class

Previously, a `class` component in React is one of the few way that you can hold state in your components. In a class component, there will be lifecycle methods to determine if the component should update the rendered elements.

<p>
<div>Commonly used lifecycle methods are:  </div>
<div><code>componentDidMount</code>: invoked when the component is mounted on to the tree</div>
<div><code>componentDidUpdate</code>: invoked immediately after the update occur</div>
<div><code>componentWillUnmount</code>: invoked immediately before the component is unmounted. Invalidating timeres, canceling network request or cleaning up subscription is done in this method</div>
</p>

A class component with state, props (more on that later) and methods will look something like this.

```js
class CatFeeder extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      batteryLife: 93,
    };
  }

  componentDidMount() {
    this.feedCats();
  }

  componentDidUpdate(prevProps, prevState) {
    if (
      this.state.batteryLife !== prevState.batteryLife &&
      this.state.batteryLife === 0
    ) {
      this.props.switchOff();
    }
  }

  feedCats() {
    this.setState(state => {
      return { batteryLife: state.batteryLife - 5 };
    });
  }

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

    return (
      <div>
        <h1>The ultimate cat feeder</h1>
        <p>Currently feeding: {catToFeed}</p>
      </div>
    );
  }
}
```

## Functional components

As we can see from above, a class component of React can become verbose quickly with a lot of boilerplate code. Another type of component, we can write is `functional components`.

As the name suggest, we will write functional components as a function declaration or a fat arrow function. Previously, functional components are pure presentational components that do not hold state. However, since the introduction of React hooks (e.g. `useState` & `useEffect` in the example below), we are now able to hold state in the functional components. The community at large, seems to have move to favour more in functional components and hooks over class components.

A function compoennt with state, props, will look something like this.

```js
const CatFeeder = props => {
  const { switchOff, catToFeed } = props;
  const [batteryLife, setBatteryLife] = useState(null);

  useEffect(() => {
    feedCats();
  }, []);

  function feedCats() {
    setBatteryLife(prevBatteryLife => prevBatteryLife - 5);
  }

  useEffect(() => {
    if (batteryLife === 0) {
      switchOff();
    }
  }, [batteryLife]);

  return (
    <div>
      <h1>The ultimate cat feeder</h1>
      <p>Currently feeding: {catToFeed}</p>
    </div>
  );
};
```

## Hooks, state, and props

At this point, you might be wondering, what exactly is the difference among hooks, state and props. These are the fundamental building block of React. In fact, I will go further to say that, on most web app, besides building the interface, you are mainly managing the browser state, either persistent (browser storage) or non persistent (web memory).

### State and props

As the name mention, state is a piece of state held by the component. It is seen in `this.state` in the `class` component and `useState` in the functional components. React employs a unidirectional data flow. A state in a parent component can be passed down as `props` in the child component.

Let's imagine that we composite our CatFeeder as part of the component of the whole CatFeederMachine. The parent component `CatFeederMachine` will holds the `isOn` state. The methods to control the `isOn` state is defined in the parent component and pass down to the child components as props.

<img src="https://res.cloudinary.com/thegeekwing/image/upload/v1584778134/blog/2020/03/reactjs/react-state-props.jpg">

### Hooks

Now that you understand the difference between `state` and `props`, we can start to grasp the concept of `hooks`.

Hooks greatly simplify our component logic and promote reusability of the stateful logic.

<p>
<div>Commonly used hooks are </div>
<div><code>useState</code>: store state. As seen above, a useState hook destruct to [state, setState]</div>
<div><code>useEffect</code>: trigger sideEffect when there is a change. Setting state in useEffect might sometimes cause some rendering issue.</div>
<div><code>useReducer</code>: store state and return state with a dispatch method. Is usually used for more complex methods. </div>
<div><code>useContext</code>: a context object that allows the value to be passed around components that are children of the context</div>
</p>

Besides the hooks api, you can also define your own custom to share logic between components. More will be shared on a separate tutorial.

These are what I think are good for dipping your toes into React ecosystem. There are many more topics that we have not covered, like folder structure, flux system and common practices and models in React. If you enjoy this tutorial, stay tuned to my future articles on more advanced React concepts.