Created
February 5, 2021 08:16
-
-
Save efstathiosntonas/4786007afbadd69205acf0ca5b21e01a to your computer and use it in GitHub Desktop.
Force TextInput to focus when using react-navigation
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
const [isFocused, setIsFocued] = React.useState(true) | |
const setFocus = React.useCallback(() => { | |
setIsFocued(true) | |
}, []) | |
const setBlur = React.useCallback(() => { | |
setIsFocued(false) | |
}, []) | |
const navi = useNavigation() | |
React.useEffect(() => { | |
navi.addListener('focus', setFocus) | |
navi.addListener('blur', setBlur) | |
return () => { | |
navi.removeListener('focus', setFocus) | |
navi.removeListener('blur', setBlur) | |
} | |
}, []) | |
return ( | |
<View> | |
{focused && <TextInput autoFocus />} | |
</View> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
credit: react-navigation/react-navigation#6918 (comment)
bug: react-navigation/react-navigation#6918