opendevs logo

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 and toggleValue 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  DevCoin image
Coding Area
opendevs logo
CoursesFAQsContact UsPrivacy PolicyTerms Of Service
Copyright © 2023 opendevs
Learn ● Develop ● Ace
All illustrations by Storyset