Created
October 30, 2019 18:50
-
-
Save wvteijlingen/195d2dcb7a8584318ed877d4f2a8cb47 to your computer and use it in GitHub Desktop.
react-native-use-toggle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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