Table of contents
No headings in the article.
Here are 5 things Redux has to do
- It has to hold content of your state by creating the store
//we do that by creating a store
const store = createstore(reducer);
// if this doesnt work then probably use configureStore or legacy_createStore
- It has to allows us to access states by using .getState() function
console.log('Initial State', store.getState())
- It has to create listeners to listen updates
// We do that with a .subscribe method
const subscribe = store.subscribe(() =>
console.log('Updated State', store.getState())
);
- It has allows us to update state
// we can dispatch() function
store.dispatch(buyCake());
- And finally use those listeners to listener to listen for those changes
subscribe();// we just called the subscribe from responsibility no 3.
The entire code will look something like this.
//we do that by creating a store
const store = createstore(reducer);
// if this doesnt work then probably use configureStore or legacy_createStore
console.log('Initial State', store.getState())
const subscribe = store.subscribe(() =>
console.log('Updated State', store.getState())
);
//I called dispatch multiple time just because I can ๐
store.dispatch(buyCake());
store.dispatch(buyCake());
store.dispatch(buyCake());
store.dispatch(buyCake());
subscribe();
ย