Reputation: 9560
class Clock extends React.Component {
render() {
return (
<div>
<MyComponent/>
</div>
);
}
}
Assume that this component re-renders. Does it create a new instance of MyComponent each time it renders?
Upvotes: 2
Views: 2229
Reputation: 9560
No, once the class component created, React didn't create a new instance of it unless the component is unmounted and mount again.
You can check if the constructor is called many times when the parent component renders.
class MyComponent extends Component {
constructor(props){
super(props);
console.log("New instance of MyComponent is created");
}
}
Upvotes: 3
Reputation: 834
React compares the current Element tree structure returned from the render() method. React uses the generated keys (or assigned keys) to match each Element to a Component instance. React determines if we have new instances (A.3), removing instances (A.0.1) or are updating existing instances (A, A.0, A.0.0).
If the keys are the same, then React will pass the props to the existing instance, kicking off its Update life cycle. If we have added new Components or changed keys, React will create new instances from the Element data. These new Components then enter the Birth/Mounting phase.
Upvotes: 2