useReducer and useContext

They are cool tools in a way that you realize useState and useEffect are cool but not to the degree that useReducer and useContext simplify your codes in cleaner fashion.
I guess cool things about React in general is that once you learn new topic, you…

They are cool tools in a way that you realize useState and useEffect are cool but not to the degree that useReducer and useContext simplify your codes in cleaner fashion.
I guess cool things about React in general is that once you learn new topic, you don’t have to go back much to basics because every new concept is designed such that you can make sense of it intuitively and you can start from the point you learned it, without going back too much to basics.

useReducer and useContext are such tools that make me think ‘Oh, I didn’t have to use previous hooks (i.e. useState) that much if I could use useReducer!’ but sort of appreciate learning useState at the same time because I can make analogies to useState when learning useReducer. Learning useState definitely helped me in understanding useReducer so I could learn useReducer faster.



How it can be confusing and how I have dissected useReducer

First of all, useReducer consists of four words

const [state, dispatch] = useReducer(reducer, initialState)

Now, notice the similarity to useState hook

const [state, setState] = useState(initialState)

The only difference is it has weird things like ‘dispatch’ and ‘reducer’

To simplify and help understand the concept intuitively, my way of devising their terms are as follows:

dispatch is almost same as setState
reducer is a function that describes action and takes setState as argument to change the state


Print Share Comment Cite Upload Translate
APA
hwangs12 | Sciencx (2024-03-29T00:33:43+00:00) » useReducer and useContext. Retrieved from https://www.scien.cx/2021/08/31/usereducer-and-usecontext/.
MLA
" » useReducer and useContext." hwangs12 | Sciencx - Tuesday August 31, 2021, https://www.scien.cx/2021/08/31/usereducer-and-usecontext/
HARVARD
hwangs12 | Sciencx Tuesday August 31, 2021 » useReducer and useContext., viewed 2024-03-29T00:33:43+00:00,<https://www.scien.cx/2021/08/31/usereducer-and-usecontext/>
VANCOUVER
hwangs12 | Sciencx - » useReducer and useContext. [Internet]. [Accessed 2024-03-29T00:33:43+00:00]. Available from: https://www.scien.cx/2021/08/31/usereducer-and-usecontext/
CHICAGO
" » useReducer and useContext." hwangs12 | Sciencx - Accessed 2024-03-29T00:33:43+00:00. https://www.scien.cx/2021/08/31/usereducer-and-usecontext/
IEEE
" » useReducer and useContext." hwangs12 | Sciencx [Online]. Available: https://www.scien.cx/2021/08/31/usereducer-and-usecontext/. [Accessed: 2024-03-29T00:33:43+00:00]
rf:citation
» useReducer and useContext | hwangs12 | Sciencx | https://www.scien.cx/2021/08/31/usereducer-and-usecontext/ | 2024-03-29T00:33:43+00:00
https://github.com/addpipe/simple-recorderjs-demo