react中ref的使用

在react中获取真实dom的时候就需要用到ref属性,具体使用如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var MyComponent = React.createClass({
handleClick: function() {
console.log(this.input)
},
render: function() {
return (
<div>
<input type="text" ref={(input) => {this.input = input}} />
<input type="button" value="Focus the text input" onClick={this.handleClick} />
</div>
);
}
});

ReactDOM.render(
<MyComponent />,
document.getElementById('example')
);
#

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×