Skip to content

Instantly share code, notes, and snippets.

@wvteijlingen
Created October 30, 2019 18:50
Show Gist options
  • Save wvteijlingen/195d2dcb7a8584318ed877d4f2a8cb47 to your computer and use it in GitHub Desktop.
Save wvteijlingen/195d2dcb7a8584318ed877d4f2a8cb47 to your computer and use it in GitHub Desktop.
react-native-use-toggle
import { useState } from "react"
export function useToggle(initialState: boolean = false): [boolean, () => void] {
const [state, setState] = useState<boolean>(initialState)
return [
state,
() => setState(!state)
]
}
export function useBooleanState(initialState: boolean = false): [boolean, () => void, () => void] {
const [state, setState] = useState<boolean>(initialState)
return [
state,
() => setState(true),
() => setState(false)
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment