我一直在研究ReactJS,但对React Native来说是全新的。我正在尝试在TextInput上使用useRef
,但是没有用。
const Playground = () =>{
const inputName = useRef();
const addName = e => {
Alert.alert(inputName.current.value);
}
return (
<SafeAreaView>
<TextInput ref={inputName} placeholder="name"></TextInput>
<Button title="add" onPress={addName}></Button>
</SafeAreaView>
);
}
export default Playground;
使用上面我在ReactJS中使用的代码,当按下addName
按钮时,它总是返回空字符串。我也尝试如下进行useEffect
,但结果相同。
useEffect(() => {
if(!inputName.current) return;
Alert.alert(inputName.current.value);
},[inputName.current])