[英]How can you create a functional component that updates on Redux store change?
For my application, I want to create a component who's sole purpose is to update when there is a change to the Redux store. 对于我的应用程序,我想创建一个组件,其唯一目的是在Redux存储库发生更改时进行更新。
I have achieved this with a componentDidMount()
that checks if the component's state matches the store and sets the state if not. 我用componentDidMount()
实现了这一点,它检查组件的状态是否与存储匹配,如果不匹配则设置状态。
My solution works , and is suitable enough for the situation, but it seems like a lot of code for a simple function. 我的解决方案有效 ,并且适用于这种情况,但对于一个简单的函数来说似乎有很多代码。
How can one achieve the same outcome, a component that updates on Redux store change, using a functional component or a more simple stateful component? 如何使用功能组件或更简单的有状态组件实现相同的结果,更新Redux存储更改的组件?
Update: Thanks to the comments below, I have a new working the passes state information as props. 更新:感谢下面的评论,我有一个新的工作传递状态信息作为道具。
React components renders due to changes in props and state, therefore when you connect your component to Redux store and supply mapStateToProps, when redux store update the mentioned props that you will return from that function, your component will re-render. React组件由于props和state的更改而呈现,因此当您将组件连接到Redux存储并提供mapStateToProps时,当redux store更新您将从该函数返回的提到的props时,您的组件将重新呈现。 You can detect changes via componentWillReciveProps life cycle method. 您可以通过componentWillReciveProps生命周期方法检测更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.