react-nativereduxredux-framework

Subscribe to single property change in store in Redux


In Redux I can easily subscribe to store changes with

store.subscribe(() => my handler goes here)

But what if my store is full of different objects and in a particular place in my app I want to subscribe to changes made only in a specific object in the store?


Solution

  • There is no way to subscribe to part of the store when using subscribe directly, but as the creator of Redux says himself - don't use subscribe directly! For the data flow of a Redux app to really work, you will want one component that wraps your entire app. This component will subscribe to your store. The rest of your components will be children to this wrapper component and will only get the parts of the state that they need.

    If you are using Redux with React then there is good news - the official react-redux package takes care of this for you! It provides that wrapper component, called a <Provider />. You will then have at least one "smart component" that listens to state changes passed down by the Provider from the store. You can specify which parts of the state it should listen to, and those pieces of the state will be passed down as props to that component (and then of course, it can pass those down to its own children). You can specify that by using the connect() function on your "smart" component and using the mapStateToPropsfunction as a first parameter. To recap:

    Wrap root component with Provider component that subscribes to store changes

    ReactDOM.render(
      <Provider store={store}>
        <App />
      </Provider>,
      document.getElementById('root')
    )
    

    Now any child of <App /> that is wrapped with connect() will be a "smart" component. You can pass in mapStateToProps to pick certain parts of the state and give it those as props.

    const mapStateToProps = (state) => {
        return {
            somethingFromStore: state.somethingFromStore
        }
    }
    
    class ChildOfApp extends Component {
        render() {
            return <div>{this.props.somethingFromStore}</div>
        }
    }
    
    //wrap App in connect and pass in mapStateToProps
    export default connect(mapStateToProps)(ChildOfApp)
    

    Obviously <App /> can have many children and you can pick and choose which parts of the state the mapStateToProps should listen to for each of its children. I'd suggest reading the docs on usage with React to get a better understanding of this flow.