React feels like a good. But it also feels impossible to use/learn sometimes.
Its main design goal is to reduce DOM changes to improve rendering times.
And an important side effect of that is that it becomes easier to do stuff of the type:
and then the new comment easily gets the callback attached to it.
- user creates a new comment that appears on screen without page reload
And it also ends up naturally doubling as a template engine.
But React can also be extremelly hard to use. It can be very hard to know what you can and cannot do sometimes, then you have to stop and try to understand how react works things better:
The biggest problem is that it is hard to automatically detect such errors, but perhaps this is the same for other frontend stuff. Though when doing server-side rendering, the setup should really tell you about such errors, so you don't just discover them in production later on.
- cannot update a component while rendering a different component warning in React
- Rendered more hooks than during the previous render.
- cannot use hooks from helpers:
Is is also very difficult to understand precisely why hooks run a certain number of times.
Examples under: web-cheat/react.
- web-cheat/react/hello.html: minimal example. As you click:By opening a web inspector, you can see that only modified elements get updated. So we understand that JSX parses its "HTML-like" into a tree, and then propagates updates on that tree.
By looking at the terminal, we see that
- one counter increments every time
- the other increments every two clicks
render()does get called every time the button is clicked, so the tree gets recreated every time.
- web-cheat/react/hello-hook.html: Hello World with hooks instead of classes, but also that is fully correct and optimized. Exactly equivalent to web-cheat/react/hello.html.
React.useStateplays the role of the constructor.Hooks were introduced much after classes, and just require less code, so everyone is using them now instead of classes.It appears that the hello world in the React.js docs is wrong though, you need functional sets to update values based on their current value: stackoverflow.com/questions/375913/how-can-i-profile-c-code-running-on-linux/60265409#60265409, otherwise you could lose updates. The
setTimeoutexample from stackoverflow.com/questions/57828368/why-react-usestate-with-functional-update-form-is-needed/65306828#65306828 is particularly relevant. A real world case would be that but waiting for network requests to update.We cannot reproduce a quick double click faliure without the functional update however, not even with
e = document.getElementById('button');e.click();e.click().
- web-cheat/react/prop-change.html: shows what gets called as parameters flow down through the tree.By looking at the console, we see all
renderget called every time, even if
propsdidn't change, but not the constructors.After page load the console contains:
Main.constructor Main.render NotMain.constructor NotMain.render NotMain2.constructor NotMain2.renderThen, every time we click the button it adds:
handleClick Main.render NotMain.render NotMain2.renderNote how the
NotMainonly change every other click, but
renderstill gets called every time.In order to make
Reactnot re-render when there are not changes, you have to either:
- define the
shouldComponentUpdatemethod of class components
- wrap functional components in
- define the
- web-cheat/react/prop-change-hook.html: same as web-cheat/react/prop-change.html, but using hooks. The notable difference is that functional components don't have a clear constructor/render separation, the function just gets called every time. Then React does some magic to ensure that
useStatereturns the current state, except for the first render where they return the initial value.
- web-cheat/react/prop-change-hook-use-memo.html: TODO forgot if this example is useful, was tring to use
- web-cheat/react/prop-change-child.html: shows what child prop changes do not call render on parent,
Maindoes not show up on console when you click under
- web-cheat/react/hook-from-function-fail.html: TODO got some errors that seemed linked to this on a larger program, but failed to minimize them here
- web-cheat/react/hook-different-number-of-times.html: this illustrates one of the cardinal points of using hooks: you must always call them the same number of times, otherwise it fails with:
React has detected a change in the order of Hooks called by Main. This will lead to bugs and errors if not fixed.In the case of
useState, we can kind of understand why this happens: React must use the order of calls to determine which state variable to return at each point in time.
- web-cheat/react/hello-hook-use-effect.html: just checking when it gets called. Happens after every render
handleClick Main useEffect useEffect2
- TODO create a test
How React works bibliography:
- www.netlify.com/blog/2019/03/11/deep-dive-how-do-react-hooks-really-work/ shows how
uesStateworks under the hood with crazy closures