This was originally posted as a twitter thread: https://twitter.com/chrisachard/status/1179015849459507200
Does Redux have you confused?
It can be simpler with the new Redux hooks.
π₯Here's a crash course into Redux, and how you can use it with React function components:
1.
Redux gives you a central place to put your state (data) for JavaScript apps
It's most often used with React (via react-redux)
This lets you access or change your state from ANY component in your tree
2.
Your state lives in a central Redux store
That store is created with a function called a reducer
A reducer takes in a state and an action,
and returns the same or a NEW state
3.
The store is given to your app using the Provider
from react-redux
Use the provider to wrap your entire app, so that any component in your app can access the store
4.
To get data out of the store, use the useSelector
hook from react-redux
selector
is just a fancy word for: "function that gets data from the store"
useSelector takes a callback, which gets the entire redux state, and you just pick out what you need for that component
5.
Actions are plain JavaScript objects
All actions should have a 'type' key
They may also have additional keys (parameters)
6.
Actions are not "called", but are "dispatched" to the reducers
The action type
is what tells the reducer what to do (return a new state or the old one)
7.
To change data in the store, first write your reducer:
Reducers are often written with switch/case statements, but don't have to be
They just have to take in an action and a state, and return a new state
8.
It's important that reducers return a NEW state object (and not mutate the old one) so that your components will re-render when something changes
Don't set state values in reducers - only ever return a new state object with changed values
9.
To dispatch an action, use the useDispatch
hook from react-redux
call useDispatch with an action object,
which will run through the reducers,
and will potentially change the state
10.
All connected components (that call useSelector) will automatically get the new state
This is treated like props or state changing - useSelector will automatically detect changes and React will re-render the component
π TADA!
That's the basics!
Redux can be used in much more complex ways, but the core is always:
- dispatch an action to the store
- which may or may not change the state via reducers
- access that state with a selector
- and changes will automatically re-render your app
π―
Code example:
View and play around with the code used in this course on codesandbox:
https://codesandbox.io/s/redux-count-hrdtd?fontsize=14
Β
Like this crash course?
Find more on twitter: @chrisachard
Or join my newsletter: https://chrisachard.com/newsletter/
Thanks for reading!
Top comments (28)
Thank you so much. I have been trying to learn redux for the past month and was not understanding how to implement it. This is simply perfect. This is what exactly what I was looking for.
I can't Thank you enough. You are the best. πππ
Glad to help! π
Cool intro to hooks for redux!
Little comment: It would be even nicer if the code examples were not images so I could easily copy snippets (and that would also give more SEO probably). But no worries, I still enjoyed reading this.
Bernard
(And thanks for contributing to DEV)
Yeah, thatβs a good point... I do include a code sandbox link at the end, but I could put one for every step - Iβll think about that for next time (or could edit this one).
Glad it was useful!
I love the verry visual explainations with all that arrow pointer stuff :)
Glad to hear it!
Hey man! This article is awesome, I am currently learning Redux in React, Can I translate your article, learn, and then put it on the China Programming Forum website? Website Addr: segmentfault.com/ . And I'll keep original address in article.
Sure thing! That'd be neat :)
Before I read this article, I was extremely confused about Redux (coming from Vuex, you can Imagine the difference) - but this article laid it all out clearly, and now I have a decent understanding of redux. Thank you so much! :D
Thanks for good intro. I've been developing in NodeJS (and a number of other langs) for ages but React made me scratch my head. I felt old and dumb :) Now I start getting it.
BTW - what's the issue with those weird "s" characters you're using? I remember we had to write "s"-s this way at school (in Europe). How do you get these?
Glad you're starting to get it!
Ah, those "s" characters are because I turned on "ligatures" in codesandbox (which I used to make those screenshots). I think they're kind of fancy looking π
Hello everyone! Recently i'm working with redux. But i am not getting one thing and that is i created a store and reducers and dispatching an action and then state gets change and i am subscribed to the store with a callback which gets called on state change now in this callback i put in a render method which will re-render my UI but my question is what if i have multiple section in my UI suppose i have a Todo list and Movie List those two are different module depends on different data and if Todo list gets updated i have to re render the whole UI again cause i subscribed to store with render method and render method take the state and basically rerender the whole UI again in this case i just updated Todo list what i want is to just rerender UI part related to Todo list not Movie list. But i can not do that cause i am subscribed to store with render method and that method basically rerender the whole UI. Now how can i only rerender specific part of UI? Do i need to create two store? One for Todo one for Movie then does redux core principle works here cause their is not single source of truth where i have two store that means two state. Or am i getting it wrong? One more thing i am not using any UI library instead vanilla Javascript
Are you using redux with React?
If you are, then React will respond to the data update by only re-rendering components that pull out data from the store that has changed.
If you aren't using React (it sounds like you aren't maybe, because you wrote your own callback that re-renders your UI?), then I can't help much - sorry! In that case you would need to write code to figure out which parts of the UI need to update. You shouldn't need to use two stores to do that - it's possible with just one.
Thank you very much for your response i really appreciate it!
Thanks! Exactly what i was looking for :D
Amazing explanation! You're really the best, dude!)
THank you!
I found it very useful and well written, thanks