import React, { Component } from "react" export default class MyInput extends Component { // 第一種
getvalue11= () => { let hah = this.refs.zhi.value console.log("第1種", hah) } // 第2種 ref的使用
getvalue=()=>{ console.log(this.input1.value) } Valuerefs = React.createRef();//創建一個承裝ref的容器 這個容器是專門的 只能保存一個ref Myrefs一致
getvalue22=()=>{ let pwd = this.Valuerefs.current console.log("第三種", pwd.value) } render() { return ( <div> {/* 第一種 */} <input type="text" ref="zhi"></input>
<button onClick={this.getvalue11}>按鈕</button>
{/* 第二種 this.input1的實例是input*/} <input type="text" ref={(input)=>{this.input1=input}}></input>
<button onBlur={this.getvalue}>按鈕</button>
{/* 第三種 */} <input type="text" ref={this.Valuerefs}></input>
<button onClick={this.getvalue22}>按鈕</button>
</div>
) } }