useToggle Custom hook
Solidify your knowledge of Custom hooks in React by solving this problem.
Dogendar 🐶 is very fond of writing custom hooks and today he wants to write a custom hook called useToggle
. The hook is supposed to return a boolean value along with a toggler function that toggles the value on invocation. Toggle means if the value is true
, then it will be changed to false
and vice-versa.
It can be used like
const { value, toggleValue } = useToggle(true) // toggleValue() will toggle the value and rerender parent component
Help Dogendar write the custom hook given below and fulfil these requirements:
- return
value
andtoggleValue
function from the hook - initially, the hook should return the given initial value in parameters
- when
toggleValue
is called, the returned value should be toggled
Solve this medium problem to earn 2