useOnToggle
Monitors a boolean or predicate state and runs the given trueEffect or maybeFalseEffect depending on the given state value.
The false effect callback can be omitted, in which case the hook will monitor the state and always trigger the only effect provided.
useOnToggle = (
state: boolean | (() => boolean),
trueEffect: (previous: boolean, state: boolean) => void,
maybeFalseEffect?: (previous: boolean, state: boolean) => void
) => void
Parameters
| type | default value | ||
|---|---|---|---|
| required | state | boolean | () => boolean | |
| required | trueEffect | (previous: boolean, state: boolean) => void | |
| optional | maybeFalseEffect | (previous: boolean, state: boolean) => void |
Usage
import { useOnToggle } from "@objectiv/tracker-react-native";
useOnToggle(
state,
(previousState, state) => {
// this effect will trigger whenever `state` changes from `false` to `true`
},
(previousState, state) => {
// this effect will trigger whenever `state` changes from `true` to `false`
},
)
with a predicate state
useOnToggle(
() => state,
(previousState, state) => {
// this effect will trigger whenever the predicate result changes from `false` to `true`
},
(previousState, state) => {
// this effect will trigger whenever the predicate result changes from `true` to `false`
},
)
without second effect and a boolean state
useOnToggle(
state,
(previousState, state) => {
// this effect will trigger whenever `state` changes
},
)
without second effect and a predicate
useOnToggle(
() => state,
(previousState, state) => {
// this effect will trigger whenever the predicate result changes
},
)