-
Notifications
You must be signed in to change notification settings - Fork 47.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimizing componentWillReceiveProps and shouldComponentUpdate #5650
Comments
Correct me if I am wrong, but it currently works this way. The This line seems unnecessary:
As you can see by the following lines, the I could test this in my current pull request here: #5787 |
@milesj That is incorrect. @Volfied Your approach seems pretty reasonable to me. Background: I understand that it's a little bit of boilerplate, but if the order of the two lifecycles were switched, users would have the opposite problem where you are calculating the new state in Your original question was:
I don't know of a cleaner way off hand, but perhaps someone else will have ideas. Your solution seemed fine to me. On a tangential note: Keep in mind that ideally your state should not be a function of your props (you should try to keep them separate and independent). For more info on keeping props+state separate: https://facebook.github.io/react/tips/props-in-getInitialState-as-anti-pattern.html Also, lifecycle methods are intended to be an escape hatch, rather than a recurring pattern in your code. Your question is a usage question, rather than a bug in the React core. Usage questions are better answered on sites like StackOverflow, as we try to use github issues for tracking bugs in the React core. For this reason, the issue was closed. You're welcomed to continue the conversation here, or move it to a site intended for these sort of questions (like StackOverflow). Anyway, hopefully you find the above info useful. Happy coding! |
Right, right, but is there a specific reason that On a related note, the docs should probably be changed, as "Invoked when a component is receiving new props." is a bit misleading. |
It is expensive to deep compare |
React component lifecycle dictates that when new props roll in,
componentWillReceiveProps
gets called, where you can calculate the new state and set it without a second cycle. Afterwards, shouldComponentUpdate gets called and we can compare props and state of last cycle with the next one.However, if state is a product of the props, it makes more sense to check whether the props have changed before calculating the state, so unnecessary calculations can be avoided. This produces some ugly looking code where
componentWillReceiveProps
first runs the code that would otherwise be inshouldComponentUpdate
and based on that result sets a new state, or terminates.Simplified Example:
Is there a cleaner way of approaching this where no resources are wasted on unnecessary state calculations?
The text was updated successfully, but these errors were encountered: