我想const [solution, setSolution] = useState(0); 通过按下按钮来设置输入元素的值
我使用createRef或使用useRef钩子得到相同的结果
给出了不同的答案,你到底要做什么,对方法有明确的内部说明吗?
function Interface() {
const [solution, setSolution] = useState(0);
const solutionInp = useRef();
// --createRef();
const onButtonClick = () => {
// `current` points to the mounted text input element
setSolution( solutionInp.current.value)
};
return (
<input
type="text"
// value={solution}
ref={solutionInp}
// onInput={(e) => setSolution(e.target.value)}
/>
</section>
<button type="button" onClick={onButtonClick}>do</button>
)}