There are many things you could do with the apple, and every action may change the apple slightly.
Let's define the possible actions you could perform:
We can now define a reducer function to handle these actions:
functionappleReducer(state=initialApple,action){switch(action.type){case'WASH':// sets the `dirty` field to `false`return{...state,dirty:false};case'EAT':// decrements the number of remaining bites (cannot go below 0)// note that the number of bites is given as a payload in the EAT actionreturn{...state,remainingBites:Math.max(0,state.remainingBites-action.bites)};case'ROT':// changes the apple's color to brownreturn{...state,color:'brown'};// we don't know how to handle other actions,// so let's just do nothing and return the appledefault:returnstate;}}
Every time we perform (or dispatch) an action, the resulting apple object is slightly different from what it was before the action.
Store
Now that we have a reducer (list of possible actions) and an initial state (the apple), let's create a store and provide the apple as the initial state:
functionhandleChange(){constcurrentAppleState=store.getState();if(currentApple.color==='red'){console.log('Looks delicious!');}else{console.log('Looks awful, better throw it in the bin!');}}store.subscribe(handleChange);
Async actions
This timer starts when we first buy the apple and waits a whole week before dispatching the ROT action:
I hope you know how this works, but as a refresher: setTimeout takes two parameters: a function and the number of milliseconds to wait. After the number has passed, the function is called.
Hi Ogwuru, Redux is not an easy thing to tackle, this and the other comments should help :)
I'll try to introduce the core concepts of Redux (store, actions, reducers, subscriptions) with a super simple example.
Initial state
Let's say you have an apple. The apple represents your application's current state in time. Let's define the apple as a JS object:
Actions
There are many things you could do with the apple, and every action may change the apple slightly.
Let's define the possible actions you could perform:
Reducer
We can now define a reducer function to handle these actions:
Every time we perform (or dispatch) an action, the resulting apple object is slightly different from what it was before the action.
Store
Now that we have a reducer (list of possible actions) and an initial state (the apple), let's create a store and provide the apple as the initial state:
To retrieve the apple object at any time, we can use store.getState(), which returns
Subscribe
Let's also subscribe to any changes to the apple:
Async actions
This timer starts when we first buy the apple and waits a whole week before dispatching the
ROT
action:I hope you know how this works, but as a refresher:
setTimeout
takes two parameters: a function and the number of milliseconds to wait. After the number has passed, the function is called.Dispatching actions
Now, let's do stuff with the apple.
Before all the actions:
After washing (
store.dispatch(WASH);
):After eating (
store.dispatch(EAT);
):After eating again (
store.dispatch(EAT);
)Let's forget about the apple for a while.
Oh, right — we used
setTimeout
to wait for a week. Once that resolves, theROT
action is dispatched and the resulting apple is this:After washing, taking 4 bites and then waiting a week, there's not much left of the apple, but hopefully you understood the basics of Redux.
You can find the whole conversation here:
Explain Redux like I'm five
hemanth.hm
Thanks Rhymes would go through it
how a 5 yo could possibly understand this..
Ahah you have a point