Skip to content

useBoolean

Nick Wylynko edited this page Nov 17, 2022 · 1 revision
import { useBoolean } from "react-raw-hooks/hooks/useBoolean"

Examples

Toggle a service running

const App = () => {
  const [running, { setTrue: on, setFalse: off }] = useBoolean(false, (run) => {
    if (run) {
      fetch('/start-service')
    } else {
      fetch('/stop-service')
    }
  })

  return (
    <div>
      <span>The Service is {running ? 'Running' : 'Stopped'}</span>
      <button onClick={on}>Start</button>
      <button onClick={off}>Stop</button>
    </div>
  )
}
Clone this wiki locally