Nikki Siapno
Nikki Siapno

@NikkiSiapno

16 Tweets Jan 23, 2023
πŸ“Œ How to Think in React β€” The Most Popular Frontend Development Framework πŸ§΅πŸ‘‡
React.js is the most popular web development library/framework πŸ‘‘
Over 44% of professional Software Developers use React.
The 2nd most used frontend web development library is jQuery at 29% (mostly legacy usage).
Let’s dive into understanding how to think in React 🧠
There’s more than one way to build something in React βš›οΈ
Here’s a guide thoughπŸ‘‡
1. Break the UI into a component hierarchy
2. Build a static version
3. Identify the complete representation of the UI state
4. Identify where your state should live
5. Add inverse data flow
1) 🌳 Break the UI into a component hierarchy 🌳
Start with the end in mind.
Identify what the end is meant to look like so that you can break down what components make up the UI πŸ’‘
Having/creating a mockup makes this easy 🎨
Draw boxes around each component & name them.
1a) How do you know what should be its own component? πŸ€”
The same way you should decide whether to create a new function or object.
The single responsibility principle - SRP πŸ’‘
A component should only do one thing βœ…
If it grows, it should be decomposed into subcomponents.
2) πŸ“„ Build a static version πŸ“„
You can build bottom-up or top-down β–²β–Ό
In small apps it’s easier to use the top-down approach, ie; start at the top of the hierarchy.
In large projects it’s easier to build bottom-up and write tests as you build πŸ§ͺ
2a) To build a static version of an app that renders a data model;
You should build components that reuse other components, & pass data via props. βœ…
State should not be used to build a static version ❌
State is reserved only for interactivity - data that changes over time.
2b) React docs suggest this decoupling approach to building (separating static & interactive).
They say it's a more efficient approach.
IMO, I use both approaches depending on what I’m building.
I suggest learning both techniques & apply what works best for the situation πŸ’‘
3) Identify the minimal (but complete) representation of the UI state
To make UI interactive, you need to be able to trigger changes to the underlying data model.
React achieves this with state πŸ™ŒπŸ’‘
3a) To build your app cleanly, keep state DRY βœ…
Think about a minimal set of mutable state that your app needs ✨
For example, for a To Do List App, keep an array of the TODO items (state). Don’t keep a state variable for the count of items, just take the length from state πŸ’‘
4) 🏑 Identify where your state should live 🏑
After identifying the minimal set of an application’s state, you need to identify which component(s) mutates, or owns state πŸ€”
4a) React is all about one-way data flow down the component hierarchy πŸ’‘
Identify every component that renders something based on a set of state.
Find a common owner component - a single component above all the components that need the state in the hierarchy.
4b) Either the common owner or another component higher up should own the state βœ…
You can then pass it down via props to all the components that also rely on the state.
If you can’t find a component where it makes sense to hold state, create a new component for holding state.
5) πŸ™ƒ Add inverse data flow πŸ™ƒ
Step 4 allows apps to render correctly as a function of props and state flowing down the component hierarchy.
Data often also needs to flow the other way; components deep in the hierarchy need to update state in components higher up 🌟
5a) To do this we pass setState() from the higher-up components down the hierarchy πŸ’‘
That way, components deeper in the tree can call setState() when required (like a form input event) to update the state living in the common owner component (a form for example) βœ…
That's a wrap!
If you enjoyed this thread, don't forget to like, comment, and retweet the first tweet!
I create threads, and hand-drawn illustrations to level up your software development game 🧡 🎨 πŸš€
Follow @NikkiSiapno
for more free tips and free resources.

Loading suggestions...