一旦按下按钮,我想要控制台记录TextInput中的文本。我想使用useRef()来引用TextInput,我在react中尝试了这一点,并且使用了fine.But --它不是在react本机中工作的
import { useRef } from "react";
import { Button, TextInput, View } from "react-native";
export default function App() {
const inputRef=useRef();
const send=()=>{
console.log(inputRef.current.value);
}
return(
<View>
<TextInput ref={inputRef}/>
<Button
title="send"
onPress={send}
></Button>
</View>
)
}
发布于 2022-10-16 08:23:28
你可以使用状态。
const [value, setValue] = useState("");
function send(){
console.log(value);
}
return (
<TextInput value={value} onChange={setValue}/>
);
https://stackoverflow.com/questions/74085518
复制相似问题