简体   繁体   English

为什么ref在子组件中不是属性?

[英]Why is ref not a property in children components?

I've recently noticed that in ReactJS passing ref to a component as property is not possible. 我最近注意到在ReactJS中无法将ref作为属性传递给组件。

Here's my Code: 这是我的代码:

class A extends React.Component{
    render(){
        return <B
            ref={ref=>this.bRef = ref} 
        />
    }
}

class B extends React.Component{
    render(){
        console.log(this.props.ref) //=>undefined
        return <div
            {...this.props}
        />
    }
}

this.bRef will be undefined in A . this.bRef将在A未定义。 Is there a explanation about this? 是否对此有解释?

Is there a explanation about this? 是否对此有解释?

The issue you are seeing is due to the fact that key and ref are special props. 您看到的问题是由于keyref是特殊道具这一事实造成的。 See 看到

Most props on a JSX element are passed on to the component, however, there are two special props (ref and key) which are used by React, and are thus not forwarded to the component. JSX元素上的大多数道具都传递给组件,但是,React使用了两个特殊的道具(ref和key),因此不转发给组件。

For instance, attempting to access this.props.key from a component (ie, the render function or propTypes) is not defined. 例如,未定义尝试从组件(即渲染函数或propTypes)访问this.props.key的情况。 If you need to access the same value within the child component, you should pass it as a different prop (ex: <ListItemWrapper key={result.id} id={result.id} /> ). 如果需要在子组件中访问相同的值,则应将其作为其他<ListItemWrapper key={result.id} id={result.id} />传递(例如: <ListItemWrapper key={result.id} id={result.id} /> )。 While this may seem redundant, it's important to separate app logic from reconciling hints. 尽管这似乎是多余的,但将应用程序逻辑与协调提示分开很重要。

To access ref in child pass it in a different prop. 要访问子级中的ref,请以其他方式传递它。 Say you create a ref like 假设您创建了一个类似的ref

const ref = React.createRef();

and then you pass it to your component as shown below: 然后将其传递到组件,如下所示:

<FancyButton forwardedRef={ref} text="Click ME!" />

where inside the FancyButton the ref will be available as FancyButton内的FancyButton ,引用将作为

 <button
      onClick={() => {
        this.logRef(forwardedRef);
      }}
      ref={forwardedRef}
    >
      {text}
 </button>

where logRef is defined as 其中logRef定义为

logRef(myref) {
    console.log(myref.current);
}

a working example can be seen at (Click on the button and check the console) : https://codesandbox.io/s/ko6wnrorv 可以在(单击按钮并检查控制台)上看到一个工作示例: https : //codesandbox.io/s/ko6wnrorv

ref does not behave like a regular props. 裁判的行为不像常规道具。 It has a special meaning. 它有特殊的含义。

class A extends React.Component{
    render(){
        return <B
            ref={ref=>this.bRef = ref} 
        />
    }
}

what it means here is, you are getting a reference of component 'B' in component 'A'. 这意味着,您正在获得组件“ A”中组件“ B”的引用。 That is, you can use this.bRef to call some methods in component 'B' from component 'A' 也就是说,您可以使用this.bRef从组件“ A”调用组件“ B”中的某些方法

Note: this.bRef will get it's value only after A and B is rendered until then it will be undefined. 注意:只有在渲染A和B之后,this.bRef才会获得它的值,直到它未定义为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM