Let's create a custom hook for tracking the counter state and exposing the current counter value with increment, decrement handlers.
Tik, Tok, Tik, Tok
- We set the initial value of the
counterby using an
initialCounterfunction parameter and if it's not provided, we use 0 as a default.
handleDecrementhandlers are updating the value of
- IMPORTANT: We are relying on the previous value of the
counterwhile updating it, hence we use a function to access the previous state.
- For simplicity, we are also exposing the
isDecrementDisabledboolean to disable the decrement button when the value of the
- Inside of the
App.jsfile, we are using our newly created, custom
useCounterhook with 10 as an initial
countervalue and build AN ACTUAL application thanks to it which you could saw in the above preview.
- How cool is that to build custom hooks? 😎