小编Ahm*_*ber的帖子

导航到屏幕时无法聚焦 TextInput

我有一个react-native-paper TextInput,当我导航到屏幕时(使用react-native-navigation),我想自动聚焦它。我尝试过autoFocus={true}在 TextInput 上进行设置,但没有成功。

在另一次尝试中,我尝试通过监听屏幕上的“焦点”事件来手动聚焦它,但这仅在我第一次打开屏幕时聚焦。有什么办法让它可靠地工作吗?

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>
  )
}
Run Code Online (Sandbox Code Playgroud)

reactjs react-native react-navigation react-hooks react-native-paper

2
推荐指数
1
解决办法
2496
查看次数