5 Responsibilities of Redux

5 Responsibilities of Redux

ยท

1 min read

Table of contents

No heading

No headings in the article.

Here are 5 things Redux has to do

  1. 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
  1. It has to allows us to access states by using .getState() function
console.log('Initial State', store.getState())
  1. 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())
);
  1. It has allows us to update state
// we can dispatch() function
store.dispatch(buyCake());
  1. 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();
ย