I guess this is a simple question but here goes: Is there a way of instructing your ponent to re-render when the props change? I have my ponent connected to the redux store which on a certain action updates the state, which then filters down into the props, upon which point I want the app to respond to the changes.
I guess you would use ponentDidUpdate()
?
Something like:
// ...
ponentDidUpdate(prevProps, prevState) {
if (prevProps !== this.props) {
// re-render x <-- not sure how to do this
}
}
Plus what would be the method to re-render the whole ponent, and what would be the method of re-rendering only the updated props?
Any help appreciated.
I guess this is a simple question but here goes: Is there a way of instructing your ponent to re-render when the props change? I have my ponent connected to the redux store which on a certain action updates the state, which then filters down into the props, upon which point I want the app to respond to the changes.
I guess you would use ponentDidUpdate()
?
Something like:
// ...
ponentDidUpdate(prevProps, prevState) {
if (prevProps !== this.props) {
// re-render x <-- not sure how to do this
}
}
Plus what would be the method to re-render the whole ponent, and what would be the method of re-rendering only the updated props?
Any help appreciated.
Share Improve this question asked Jan 18, 2017 at 20:13 Paulos3000Paulos3000 3,54511 gold badges37 silver badges68 bronze badges 1- 2 Can you update your questions with more information about the props? Your ponent should be rendering again "automatically" when changing your props (without needing to call that manually). Take a look at github./reactjs/redux/issues/585#issuement-133028621 to see if it helps – William Martins Commented Jan 18, 2017 at 20:22
2 Answers
Reset to default 3You can call this.forceUpdate()
in the method. Another way is this.setState(this.state)
.
You can use
ponentWillReceiveProps (props){
this.doSomething(props) // To some function.
this.setState({data: props}) // This will update your ponent.
}
If your props is something that needs to change the state you will get an infinity loop.
Check out Reacts life cycle here by the way, might help aswell.