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(){constcurrentApple=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.
That's a really awesome kicktstarter, Mikk. Kudos! I am already feeling that I should get on with Redux:)
I guess more folks who are well versed with Redux should recommend Redux courses here - hackr.io/tutorials/learn-redux - so that wannabe like me can start easily.
Great explanation! I wrote a quick demo of this working in an application, helped me understand how it all fits together - github.com/adam-m-holloway/explain...
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 returnsSubscribe
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
ROTaction:I hope you know how this works, but as a refresher:
setTimeouttakes 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
setTimeoutto wait for a week. Once that resolves, theROTaction 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.
Dang, that's a fairly simple way to explain it. Well done.
When you said...
I think you may have a typo where you used
WAITinstead ofROT, which you defined earlier. If that's the case, you also reference it here:If not, never mind me.
Cheers!
Oh, you are correct. I did rename the action at one point and missed a few spots. Thanks!
Happy to help. :)
That's a really awesome kicktstarter, Mikk. Kudos! I am already feeling that I should get on with Redux:)
I guess more folks who are well versed with Redux should recommend Redux courses here - hackr.io/tutorials/learn-redux - so that wannabe like me can start easily.
Great explanation! Now you've made me want to start using redux for a new project...
This made perfect sense! Thank you.
many thanks for this. It made me understand the flow of redux better. haha
What a great Explanation! Can you do this for Explain React like I'm five, please?
Really appreciate.
Thanks! React has already been explained here: dev.to/tiffanywismer/explain-react...
Great explanation! I wrote a quick demo of this working in an application, helped me understand how it all fits together - github.com/adam-m-holloway/explain...
Here is the working APP deployed to surge.sh Redux App like 5
Here is the working APP deployed to surge.sh Redux App like 5
I really loved it. Thank you so much