代码
<div id="test"></div>
<script type="text/babel">
'use strict'
class Demo extends React.Component{
myRef = React.createRef();
showDate=()=>{
alert(this.myRef.current.value);
}
render(){
return(
<div>
<input ref={this.myRef} type = "text" placeholder = "点击按钮提示数据"></input>
<button onClick = {this.showDate}>点我提示左侧的数据</button>
</div>
)
}
}
ReactDOM.render(<Demo/>,document.getElementById('test'));
</script>
注意
- 该容器只能保存一个结点,若后面再使用该容器,则会被覆盖
- myRef容器中保存的是一个键值对,键为current,值为该结点