简体   繁体   中英

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.

I have achieved this with a componentDidMount() that checks if the component's state matches the store and sets the state if not.

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?


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. You can detect changes via componentWillReciveProps life cycle method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM