简体   繁体   English

如何创建一个在Redux商店更新时更新的功能组件?

[英]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.

相关问题 在功能组件中,如何使用 react-redux 连接从 redux 商店访问道具? - In a Functional Component, how do you access props from the redux store using react-redux connect? 如何从功能组件向 redux 存储发送数据? - How to send data to redux store from functional component? React class 组件和 React 功能组件之间的 redux 存储更改后访问道具的差异 - Difference in accessing props after change in redux store between React class component and React functional component 如何从非组件辅助函数访问redux的存储? - How can you access redux's store from a non-component helper function? Redux Component不会在商店更改时更新 - Redux Component will not update on store change ReactJS - 如何将 Class 组件与组合 state 更新到功能组件的副作用 - ReactJS - How can I transform a Class Component withsSide-effects of combined state updates to a Functional Component 如何使用 useSelector 在功能组件中访问 redux 的 store state 变量? - How to access redux's store state variable in a functional component using useSelector? 功能部件的redux挂钩 - redux hooks for functional component React Redux-存储更新,重新渲染组件……但不包含新数据 - React Redux - store updates, component re-renders… but not with new data redux 存储更新时组件未按预期重新呈现 - component not re-renders as expected when redux store updates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM