【发布时间】:2017-11-03 16:35:20
【问题描述】:
我知道这个问题已经在堆栈溢出中得到解决,但对我来说,我有更具体的要求。我想在子组件中定义道具的值,并能够将其值传递给其祖父组件。 我要定义值的子组件:
const Child = React.createClass(
onselect(status) {
let testProp = {refs: this.refs.statetext, status:status}
}
render() {
return (
<Mycomp ref="statetext"/>
);
});
家长比较:
const Parent = React.createClass(
//can I access the refs.statetext here???
render() {
return (
<Child/>
);
});
祖父母比较:
const GrandParent = React.createClass(
//here I'd like to access the testProp defined in the Child comp
closeMenu() {
//should have the testProp value here to do some action in this function.
}
render() {
return (
<Parent onScroll={this.closeMenu.bind(this)}/>
);
});
这可能吗?
【问题讨论】:
-
它没有回答你的问题,但 ref 通常是一种不好的反应实践,你只是不思考反应。为什么不在您的祖父母中启动它或使用 storeManager ?
-
@Nevosis 使用
ref在某些情况下很好,与“在 React 中思考”没有任何关系。如果你想要一个不受控制的输入,你必须使用ref,例如。
标签: reactjs