I have a react-native-paper TextInput that I want to focus automatically when I navigate to a screen (using react-native-navigation). I have tried setting autoFocus={true}
on the TextInput, but that didn't work.
In another attempt, I tried to focus it manually by listening to the 'focus' event on the screen, but that only focused it the first time I opened the screen. Is there any way of getting it to work reliably?
export default function NewAccountScreen({ navigation }) {
const [name, setName] = useState('');
const textInputRef = createRef();
// This isn't working, neither is autoFocus...
const focusOnInput = () => {
textInputRef.current?.focus();
}
navigation.addListener("focus", focusOnInput);
return (
<View>
<TextInput ref={textInputRef} label="Account name" value={name} onChangeText={setName}/>
</View>
)
}