使用 ref 对已渲染到页面的节点进行标记
生活随笔
收集整理的這篇文章主要介紹了
使用 ref 对已渲染到页面的节点进行标记
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、字符串形式的 ref
<input type="text" /*給標(biāo)簽打上標(biāo)記 ipt */ ref="ipt" />
拿到標(biāo)記的節(jié)點(diǎn),對(duì)其進(jìn)行操作
二、回調(diào)函數(shù)形式的 ref
handleClick = () => {const {ipt} = this; // 結(jié)構(gòu)出 this 上的 iptconsole.log(ipt); }render() {return (<div><input type="text" ref = {c => this.ipt = c {/* 此時(shí)的 c 就是 input 元素本身 */}}/> {/* this.ipt 在實(shí)例上呈現(xiàn) ipt: input */}<button onClick={this.handleClick}>按鈕</button></div>) }三、createRef 形式
// 標(biāo)記節(jié)點(diǎn) myRef = React.createRef(); myRef2 = React.createRef();handleClick = () => {// this.myRef 是一個(gè)對(duì)象// this.myRef.current === input 元素本身console.log(this.myRef.current.value); // 拿到當(dāng)前被標(biāo)記的節(jié)點(diǎn)的值 }render() {return (<div><input type="text" ref={this.myRef} /><button onClick={this.handleClick}>按鈕</button><input type="text" ref={this.myRef2} onBlur={this.handleBlur}/></div>) }總結(jié)
以上是生活随笔為你收集整理的使用 ref 对已渲染到页面的节点进行标记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 日志库EasyLogging++学习系列
- 下一篇: php获取40001,php - Dis