Skip to content

Instantly share code, notes, and snippets.

@efstathiosntonas
Created February 5, 2021 08:16
Show Gist options
  • Save efstathiosntonas/4786007afbadd69205acf0ca5b21e01a to your computer and use it in GitHub Desktop.
Save efstathiosntonas/4786007afbadd69205acf0ca5b21e01a to your computer and use it in GitHub Desktop.
Force TextInput to focus when using react-navigation
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>
)
@efstathiosntonas
Copy link
Author

efstathiosntonas commented Feb 5, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment