admin管理员组文章数量:1392050
The ref
property enables us to capture the value of an uncontrolled ponent.
class MyComponent extends Component {
render() {
<input type="text" ref={el => this.setState({ myEl: el })}/>
}
}
How does this work? Presumably input
is actually a React ponent that has a property ("prop") ref
that takes a callback that is invoked with the wrapper ponent of the field every when ponentDidMount
is called?
The ref
property enables us to capture the value of an uncontrolled ponent.
class MyComponent extends Component {
render() {
<input type="text" ref={el => this.setState({ myEl: el })}/>
}
}
How does this work? Presumably input
is actually a React ponent that has a property ("prop") ref
that takes a callback that is invoked with the wrapper ponent of the field every when ponentDidMount
is called?
-
3
BTW don't mutate state directly! Use
setState
– Andrew Li Commented Jun 23, 2017 at 12:45
1 Answer
Reset to default 7From the React documentation:
Adding a Ref to a DOM Element
React supports a special attribute that you can attach to any ponent. The
ref
attribute takes a callback function, and the callback will be executed immediately after the ponent is mounted or unmounted.[...]
React will call the
ref
callback with the DOM element when the ponent mounts, and call it withnull
when it unmounts.
So the ref
callback is called after the ponent is mounted to the DOM, with the underlying DOM element as the sole argument. It is also called after unmounting with the argument null
.
本文标签: javascriptHow does the ref callback work in ReactStack Overflow
版权声明:本文标题:javascript - How does the ref callback work in React? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744783290a2624845.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论