最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Re-render component on props update - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 3

You 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.

发布评论

评论列表(0)

  1. 暂无评论