【发布时间】:2021-11-13 00:18:54
【问题描述】:
当我单击编辑按钮时,我试图将焦点设置在 TextInput 上,但出现错误。
下面是我的代码:
const MyProfileScreen = (props: any) => {
const refsFocus = useRef(null);
return (
<>
<View>
<TextInput
placeholder={'editable test'}
placeholderTextColor={'red'}
style={{color: 'red'}}
ref={refsFocus}
/>
</View>
<TouchableOpacity
onPress={()=> { refsFocus.current.focus();} }
>
<Text>Edit text input</Text>
</TouchableOpacity>
</>
);
};
但我在onPress={()=> { refsFocus.current.focus()} } 上遇到错误
错误信息:
Object is possibly 'null'
const refsFocus: React.MutableRefObject<null>
我无法弄清楚我做错了什么
【问题讨论】:
标签: typescript react-native react-hooks react-native-android react-native-ios