InterviewSolution
| 1. |
What Does Shouldcomponentupdate Do And Why Is It Important? |
|
Answer» Above we TALKED about reconciliation and what React does when setState is called. What shouldComponentUpdate does is it’s a lifecycle method that allows us to opt out of this reconciliation process for certain components (and their child components). Why would we ever want to do this? As mentioned above, “The END goal of reconciliation is to, in the most efficient way possible, update the UI based on new STATE”. If we know that a certain section of our UI isn’t going to change, there’s no reason to have React go through the TROUBLE of trying to figure out if it should. By RETURNING false from shouldComponentUpdate, React will assume that the current component, and all its child components, will stay the same as they currently are. Above we talked about reconciliation and what React does when setState is called. What shouldComponentUpdate does is it’s a lifecycle method that allows us to opt out of this reconciliation process for certain components (and their child components). Why would we ever want to do this? As mentioned above, “The end goal of reconciliation is to, in the most efficient way possible, update the UI based on new state”. If we know that a certain section of our UI isn’t going to change, there’s no reason to have React go through the trouble of trying to figure out if it should. By returning false from shouldComponentUpdate, React will assume that the current component, and all its child components, will stay the same as they currently are. |
|