I have seen the two approaches : in this example, which is taken from a course by Dan Abramov, he is using this approach :
const render = () => {
ReactDOM.render(
<Counter
value={store.getState()}
onIncrement={() =>
store.dispatch({
type: 'INCREMENT'
})
}
onDecrement={() =>
store.dispatch({
type: 'DECREMENT'
})
}
/>,
document.getElementById('root')
);
};
store.subscribe(render);
The store.subscribe() function in Redux allows to add listeners that get called when an action is dispatch.
in this other example, which is an example from Redux documentation :
render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)
the store.subscribe is not used, but instead they wrap the entire App inside a <Provider>
component.
What’s the difference between the two approaches ? It seems they are doing the same thing, which is to make sure the App has the last version of the state.
Can/Should I use Store.subscribe if i have wrapped my app with a <Provider>
?