In React, if father component "A" re-rendered and change the position of its son component "B", but B's props and states aren't changed, then will the "B" be re-rendered? If it will, why is that necessary?
-
1Hi Ethan, "rendering" can be confusing to those new to react. Do you mean if `render` method would be called or if `dom` would be re-rendered? – dubes Mar 14 '19 at 08:04
-
@dubes , aren’t they the same thing? The render() being called and DOM being re-rendered... – Ethan Wang Mar 14 '19 at 09:41
-
No, they are different, don't worry it also had me confused. I just wrote an answer to show the difference between `render` & `painting` , perhaps that helps: https://stackoverflow.com/a/55157980/1695393 – dubes Mar 14 '19 at 09:57
2 Answers
I try to give an answer, with an example that can show what I'm saying. Though, you may want to wait for other people to give other answers too.
Short story: yes, the child Component will be re-rendered, and that's because, being a child of a Component that is being re-rendered, its render()
method will be called because of a sort of "waterfall effect": each Component inside a render()
method can be seen as a function, thus, if the render()
method is called, all the functions inside it are called again, leading to a re-render.
Though, what's important is that, even if the child Component is re-rendered, this does not mean that the DOM will change! Actually, that will not happen, and that's because of the React reconciliation: https://reactjs.org/docs/reconciliation.html.
Basically, React is smart enough to see if something has changed in the DOM, and replace the DOM element that actually needs to change (this is really semplified).
Now, about the example, look at this fiddle:
class Child extends React.Component {
/* Un-commeting this function, you can see that the render() method is not called agian.
shouldComponentUpdate(nextProps, nextState) {
if (JSON.stringify(nextProps) === JSON.stringify(this.props) &&
JSON.stringify(nextState) === JSON.stringify(this.state)) {
return false;
}
return true;
}*/
render() {
console.log("Child's rendering");
return (
<p>Child says "Hello World"</p>
);
}
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {show: false};
}
render() {
return (
<div>
<button onClick={() => this.setState({show: !this.state.show})}>Toggle</button>
{this.state.show && <p>Parent says: Hello World!</p>}
<Child />
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
@import url(https://fonts.googleapis.com/css?family=Montserrat);
body {
font-family: 'Montserrat', sans-serif;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id='root'></div>
Each time you click on the button, in the console will be rendered the message Child's rendering
, thus the Child
Component is running its render()
method.
BUT! If you inspect the DOM, when you click the button and the message `Parent says: Hello World" appears on the screen, this is what's happening in the DOM:
As you can see, only the <p>
element with the message is changing in the DOM!.
Instead, when you click again the button and the message goes away, this happens:
In this case is the parent <div>
element that is changing, and only it, and that's because we have deleted one of its child.

- 1,678
- 2
- 18
- 37
-
1Good answer. I would just like to add, it is absolutely fine if react calls `render` multiple times, react won't do anything if it returns the same thing, so from our side, we just need to make sure that our `render` method is not doing something resource intensive or blocking (such as fetching data), it is just a javascript function that is being called. – dubes Mar 14 '19 at 08:35
-
Absolutely! And I forgot to add that you can actually prevent to run the `render()` again by overriding the `shouldComponentUpdate()`. I've edited the fiddle implementing the function – Jolly Mar 14 '19 at 08:48
-
Be careful about preventing the render though, it will prevent rendering everything in the component tree below it. My point was if you keep your render method simple & free of time consuming logic, the overhead is negligible – dubes Mar 14 '19 at 08:52
-
Thanks, This exactly solves my problem, by the way commented block first return is "false", I'm a beginner and last time when I use shouldcomponentupdate the inside code gave me an error, this time I use your code in my project it just works. The result is amazing for me that even though the child changes its position, it can also not be rendered again – Ethan Wang Mar 14 '19 at 09:26
-
You're welcome! And you're right, it was `return false` (I made a test with `true` and then left it, my bad). Read what @dubes since he's right, you must really pay attention using `shouldComponentUpdate()`. – Jolly Mar 14 '19 at 09:40
Its necessary Because we’re working with JavaScript, we can change children. We can send special properties to them, decide if we want them to render or not and generally manipulate them to our will.
You can use React's PureComponent
if don't want to update Child Component
every time when Parent Component
updated
You can use shouldComponentUpdate to prevent a rerender of the child component. It can be used to prevent component renderings on a fine-grained level:
You can apply equality checks for different props and state, but also use it for other kind of checks. However, imagine you are not interested in checking each incoming prop by itself, which can be error prone too, but only in preventing a rerendering when nothing relevant (props, state) has changed for the component. That’s where you can use the more broad yet simpler solution for preventing the rerender: React’s PureComponent
.
import React, { Component, PureComponent } from 'react';
...
class Square extends PureComponent {
render() {
return <Item>{this.props.number * this.props.number}</Item>;
}
}
React’s PureComponent does a shallow compare on the component’s props and state. If nothing has changed, it prevents the rerender of the component. If something has changed, it rerenders the component.

- 1,151
- 1
- 19
- 31
-
1I don't think that answers the question. The question is about if and why a child gets re-rendered when parent changes – dubes Mar 14 '19 at 08:03