Learning Redux
上QQ阅读APP看书,第一时间看更新

Subscribing to state changes

To subscribe to changes in the store, we simply call store.subscribe() with a function that will be executed when the store changes. For example, we could log every state change:

const unsubscribe = store.subscribe(() => {
console.log('state changed:', store.getState())
})
The store.subscribe() function returns an unsubscribe() function, which can be executed to stop listening to state updates.

You might have noted that this code does not produce any output yet. The initialization process of the Redux store does not trigger a call to our subscribed function. We first need to dispatch an action to see the state change being logged.