Getting understand “useReducer in React” from real life sample

💖 For those who have learned about useReducer, but still can’t get when you should use intuitively

💎 I implemented toggle button by useState

const [showMenu, setShowMenu] = useState<boolean>(true)

// when I want to show/hide toggl…


This content originally appeared on DEV Community and was authored by Kaziu

💖 For those who have learned about useReducer, but still can't get when you should use intuitively

💎 I implemented toggle button by useState

const [showMenu, setShowMenu] = useState<boolean>(true)

// when I want to show/hide toggle button
setShowMenu(!showMenu)

💎 But I realised there is better way by useReducer

const [showMenu, toggleShowMenu] = useReducer((prev) => !prev, true)

// when I want to show/hide toggle button, that's all!
toggleShowMenu()

If you had like this experience "real life example of useReducer", make a comment please 😎


This content originally appeared on DEV Community and was authored by Kaziu


Print Share Comment Cite Upload Translate Updates
APA

Kaziu | Sciencx (2022-05-07T12:07:35+00:00) Getting understand “useReducer in React” from real life sample. Retrieved from https://www.scien.cx/2022/05/07/getting-understand-usereducer-in-react-from-real-life-sample/

MLA
" » Getting understand “useReducer in React” from real life sample." Kaziu | Sciencx - Saturday May 7, 2022, https://www.scien.cx/2022/05/07/getting-understand-usereducer-in-react-from-real-life-sample/
HARVARD
Kaziu | Sciencx Saturday May 7, 2022 » Getting understand “useReducer in React” from real life sample., viewed ,<https://www.scien.cx/2022/05/07/getting-understand-usereducer-in-react-from-real-life-sample/>
VANCOUVER
Kaziu | Sciencx - » Getting understand “useReducer in React” from real life sample. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/05/07/getting-understand-usereducer-in-react-from-real-life-sample/
CHICAGO
" » Getting understand “useReducer in React” from real life sample." Kaziu | Sciencx - Accessed . https://www.scien.cx/2022/05/07/getting-understand-usereducer-in-react-from-real-life-sample/
IEEE
" » Getting understand “useReducer in React” from real life sample." Kaziu | Sciencx [Online]. Available: https://www.scien.cx/2022/05/07/getting-understand-usereducer-in-react-from-real-life-sample/. [Accessed: ]
rf:citation
» Getting understand “useReducer in React” from real life sample | Kaziu | Sciencx | https://www.scien.cx/2022/05/07/getting-understand-usereducer-in-react-from-real-life-sample/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.