Day 8: Almost finished with React

kemystra

Kemystra

Posted on April 13, 2022

Day 8: Almost finished with React

The Feynman technique says that teaching a subject makes you better at it, which is what I'm trying to do here. You may correct me if you saw mistakes in this post

State updates based on previous state

Sometimes we want to update a state based on its previous... state. For example, a Count Dracula 🧛 button where each time people click it, the state increments by one (Gonna add this one on my future portfolio site.)

React is very well built, however. Too well build. React will bundle state updates to save resources. The impact is things become asynchronous, which is a massive pain in the ass to debug 🤧.

To fix this, we can use this syntax:

this.setState((state, props) => ({
  count: state.count + props.increment
}));
Enter fullscreen mode Exit fullscreen mode

The brackets around the object is just a syntax.

Stateful components, and where to find use them 🐉.

The main paradigm of React is a unidirectional data flow. It just means that data is passed down from parents to children, and children should only communicate to parents by calling functions.

So what does this means? Well, you only need a few (if not one) stateful component as parent, handling the app logic. The rest of the components only serve to handle the bling-bling ✨.

You can break down the whole app into small components, that can be controlled by a few, big stateful components.

Afterwords

The React course on FreeCodeCamp at this point focuses on more examples and practices. So tomorrow would be kinda mind-bending 💫, but almost no new thing. I dunno what to post tomorrow 🤣.

Follow me on Github!
Also on Twitter!

💖 💪 🙅 🚩
kemystra
Kemystra

Posted on April 13, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Day 16: Final SASS touch-up
100daysofcode Day 16: Final SASS touch-up

April 21, 2022

Day 13: Preparing React Practice
100daysofcode Day 13: Preparing React Practice

April 18, 2022

Day 14: Random Quote Machine
100daysofcode Day 14: Random Quote Machine

April 19, 2022

Day 8: Almost finished with React
100daysofcode Day 8: Almost finished with React

April 13, 2022

Day 20-24: Drum Machine
100daysofcode Day 20-24: Drum Machine

May 1, 2022