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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.