【发布时间】:2021-09-10 21:55:02
【问题描述】:
我有一个 react-native-paper TextInput,我想在导航到屏幕时自动聚焦(使用 react-native-navigation)。我尝试在 TextInput 上设置 autoFocus={true},但没有成功。
在另一次尝试中,我尝试通过监听屏幕上的“焦点”事件来手动对焦,但这只是在我第一次打开屏幕时才对焦。有没有办法让它可靠地工作?
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>
)
}
【问题讨论】:
标签: reactjs react-native react-hooks react-navigation react-native-paper