美文网首页
react学习(5)

react学习(5)

作者: 哆啦C梦的百宝箱 | 来源:发表于2023-12-20 13:57 被阅读0次

1:受控表单绑定
概念:使用react组件的状态(useState)控制表单的状态。


image.png
const [value,setValue] = useState('');
<input type='text' value={value}  onChange={(e)=>setValue(e.target.value)} />

2: react中获取dom(非受控的方式)
(1):使用useRef生成ref对象,绑定到dom身上。
(2):dom可用时,通过ref.current获取dom。

const inputRef = useRef(null);
<input type='text' ref={inputRef}/>
//console.log(inputRef.current.value);

相关文章

网友评论

      本文标题:react学习(5)

      本文链接:https://www.haomeiwen.com/subject/wdbtndtx.html