Boolean toggle using useReducer hook in React.js

38 · Amit Merchant · May 3, 2022, 5:22 p.m.
Toggles are an integral part of a web application. Toggles usually have two states. Most of the time these would be of type boolean. So, a toggle can have a value of either true or false. The counter-intuitive way Using the useReducer hook The counter-intuitive way Now, if you have ever worked with React.js, you might have implemented a toggle using the useState hook. Here’s what a simple toggle implemented using the useState hook might look like. const [isOpen, setIsOpen] = useState(false); ...