【发布时间】:2017-06-15 10:01:22
【问题描述】:
正如issue 中所建议的那样,如果我想引用子组件,建议使用 refs。
findDOMNode(childComponentStringRef)
class Field extends Component {
componentDidMount() {
// this.inputNode.focus(); // Basically I want to access the ref to input here as well
}
render() {
return (
<input type='text' ref={this.props.inputRef} />
)
}
}
class MyComponent extends Component {
componentDidMount() {
this.inputNode.focus();
}
render() {
return (
<div>
Hello, <Field inputRef={node => this.inputNode = node} />
</div>
)
}
}
我想要的是访问引用,在 Field 组件内也提供给 input。那么我们该怎么做呢?
我尝试过使用
-
this.props.inputRef
-
this.inputRef
但没有一个有效。请指导我。
【问题讨论】:
标签: javascript reactjs ref