What do the React docs mean by the following statement? - javascript

In our experience, thinking about how the UI should look at any given moment, rather than how to change it over time, eliminates a whole class of bugs.
From React Docs
From my understanding, this means that React only updates what's necessary, rather than destroying and re-constructing the entire DOM tree again. Am I wrong?
Can anyone please help me understand the quoted statement?
Thanks.

From my understanding, this means that React only updates what's necessary, rather than destroying and re-constructing the entire DOM tree again. Am I wrong?
If you want to know the short answer, I have to say it is true, React will update the necessary elements in DOM whenever it needed.
But if you want to know how it's done, and when React will update the DOM and its element I have to it is varying to different several things, like project architecture, using proper methods (proper hooks in functional component eg. useCallback, useMemo, and so on) and so on.
When it truly gets rerender then?
As far as I know, there are two ways React finds out when to rerender the DOM.
Passing elements to ReactDOM.render
Update a state
What is ReactDOM.render?
This will call the render() method from react-dom (Where we usually import it as ReactDOM) and it will render a React element into the DOM in the supplied container and return a reference to the component (or returns null for stateless components). Also if the React element was previously rendered into the container, this will perform an update on it and only mutate the DOM as necessary to reflect the latest React element.
What does state mean?
The state object is where you store property values that belong to the component. So when you got a component and that component has their own specific variables where changing them should affect the DOM you should use it, then whenever state gets changes the component will be updated.
So what I even talk about project architecture and this stuff, when I didn't mention anything about it above?
Let's say we got a project with one parent component and 3 child component just like this:
Component 1
- |- Component 2
- - |- Component 3
- - - |- Component 4
So whenever you a state in Component 4 all of the DOM elements will be get rerendered, why then? Because Component 4 is a child of Component 3 and so on, so when the child state gets change it will force the parent to rerender then the whole DOM will rerender once per state change.
Final Note
So, at last, we should be always considered a good architecture and hierarchy for our project or when it necessarily use built-in methods like useMemo to avoid such a thing.

Related

How can I reduce unnecessary re-renders in react without using the useMemo hook?

I am unsure as to whether I should change my approach on how I render the components. I depend on a lot of modals that causes unnecessary re-renders when showing/hiding the modals.
This is a basic idea of how my components are currently being rendered:
I have cases where I can re-use the components (such as Component X in Component C and D), however there are cases (Component A3/B3 and Component E3) where the structure is completely different making it difficult to reuse the component. The problem I am encountering is slow UI due to multiple re-renders.
I display the modals based on state variables declared in their corresponding parent component. When changing the parent's state variable to display the modal causes the parent to re-render, and thus all of its children too. So as an example if I want to display the Create/Edit modal in Component A1, this will cause Component A, A2 and A3 to also re-render which is quite unnecessary. I have made use of the useMemo and useCallback hooks (https://reactjs.org/docs/hooks-reference.html#usememo) to help reduce the load, but I am unsure if this is the right approach? It feels like I am depending too much on the useMemo and useCallback hooks just to display a modal.
The idea behind the modals is to delete/edit/create a record that will display in the table (located in a different component). This is by design and I cannot change this.
To manipulate the data I am passing a callback function to each modal component. The modal component sends through the data being changed in the modal to the callback function where the data can be manipulated on the parent component.
This is a case for React.memo. It is like the useMemo hook, but for components, and prevents re-renders of the component unless its props change:
const MyComponent = React.memo(function MyComponent(props) {
/* render using props */
});
If your component renders the same result given the same props, you can wrap it in a call to React.memo for a performance boost in some cases by memoizing the result. This means that React will skip rendering the component, and reuse the last rendered result.
If this doesn't solve the issue because your modals and components share the same props, then it's probably time to reconsider the structure of your state so that you can separate it according to the concerns in your application.
Alternate approaches for independent use of "global" state include the context API, and various atomic state libraries like recoil and effector.

How to see which watchers get called in Vue.js?

I have a parent component, with many nested children components inside.
A click on one of my children's components updates the route. The route property is watched on the parent component, and does some actions on change.
This can be extremely difficult to debug if I don't know which component contains the watcher. Is there a way to see which watchers get executed?
Not sure if it helps but you may use the devtools to see which components emit something.
On top of that, you could probably console.log in the watch or emit some additional payload to identify the component more easily.

React: Is a component entirely re-rendered on every setState()?

Just got a bit confused about the lifecycle in react.
Here's my understanding...
The render() always run first, right? If so...
It implies that a setState() inside useEffect() only runs after the initial render(), correct?
Question:
When the above happens, does the entire component re-render?So that would be a second time the component renders just to load a state.
Wouldn't that be a performance issue?
On every state change render is called again but not whole component renders again.
React keeps Two DOM Tree Objects in memory:
Virtual DOM
Real DOM
React have a very intelligent and powerful diffing algorithm which calculates difference between previous DOM state and Next DOM state called Reconciliation process.
Only those sub elements which have changed would be re-rendered.Keys help React identify which items have changed, are added, or are removed.Keys should added to list or array elements , to give those elements a stable identity
For example, you want to delete <input key="i42"/> element from your list so on left side Its Actual DOM Tree Object and on Right side its Virtual DOM tree object. React calculates difference between two and only the difference will be Recreated Intelligently.
https://reactjs.org/docs/lists-and-keys.html
https://reactjs.org/docs/reconciliation.html#the-diffing-algorithm
So the thing about React is that there are two DOMs -- one is the actual DOM, and the other is the virtual DOM. Every time there is a state change, the virtual DOM re-renders. React then compares the changes to the virtual DOM vs changes to the real DOM, and only updates the real DOM with what has actually changed.
Re-rendering the virtual DOM is not a performance issue as it's super quick.
There's a cool article you can read about this

How to detect rerenders in React JS correctly?

Let's say we have a parent component and multiple functional child components. I want to clearly know if the parent re-renders does the child re-renders or not.
After going through a few articles I came to know there are 3 ways we can detect rerenders. (Let me know if there are more ways.)
1. Put a console.log in the child component.
2. Use Chrome paint flashing option in the settings.
3. Use React dev tool
Do all these ways are correct to know if the component really rerenders? Because it doesn't seem to be work correctly with React.memo.
When I am wrapping the Child component with React.memo it does not print console.log, when the parent component rerenders which is correct. But still with chrome and react dev tools highlights the child component as if it rerendered.
CodeSandbox: https://codesandbox.io/s/bold-cloud-iv0rv
(If we add a new car still the static component is highlighted in green, but as per Memo, it should not rerender.)
Now my doubt, Is paint flashing does not work correctly or React.memo having issues?
Reason
If you use React.memo, you need to use it from parent down to all the child till the end.
Since React.PureComponent share the same feature with React.memo, you can find those explanation in the document as below.
Furthermore, React.PureComponent’s shouldComponentUpdate() skips prop updates for the whole component subtree. Make sure all the children components are also “pure”.
Result
By changing parent component Cars to memo
// Before
export default Cars;
// After
export default React.memo(Cars);
You could find the react-dev-tools in chrome will only show the parent component re-rendered this time as well as no child console.log fired, which is correct. (Compare to previous, all the child also got highlighted)
Before
After
Conclusion
Both console.log and react-dev-tools worked well, you may just need to implement in a proper way following your demand.

Avoiding child rerender when parent runs setState

lHello everyone, here is the problem.
We have a grid component with some filtering enabled. When the filtering is applied, if a certain callback-prop exists, it is called with the filtered data as an argument.
The problem is this. If said datagrid is wrapped by a parent component and the parent component saves the filtered data in it's state, it causes the parent to rerender, but also the datagrid. However, when the datagrid renders it runs it's filtering logic, which causes the callback(which is setState() call) to run.
So, to avoid the loop I introduced a variable to the parent component class and save the data there, but it doesn't seem so good to me.
Another option would be redux, just add a new action and dispatch it when the filtering runs.
Any other ideas?
Since you're also asking for other ideas, may I suggest React hooks. They allow finer-grained control, such as multiple states, reducers, memoized callbacks, effects that are only called when inputs change, etc.

Categories