转发 refs ——不建议过多使用,但需要了解

zhuanbike 2022-1-2 694

 class Com extends React.Component{
            constructor(props){
                super(props)
                this.myRef=React.createRef();  //React.createRef()定义ref接收
            }
            fun=()=>{
                console.log(this.myRef.current.value) //输出在对应DOM中接收到的数据
            }
            render(){
                return(
                    <div>
                        <input type="text" ref={this.myRef} placeholder="请输入"/>
                        <button onClick={this.fun}>获取收入内容</button>
                    </div>
                )
            }
        }
            ReactDOM.render(<Com />,document.getElementById("demoReact"));

      


最新回复 (0)
发新帖