javascript - React context consumer how to access ref on to the consuming component
问题描述
I've build a higher order component using the new context API of React and I am trying to to find a way to be able to access the reference (via ref()
) of a wrapped child component.
tl/dr: Essentially I want to forward the ref from the Consumer
to the actual component.
Here's a specific example. The RootComponent
sets the value for the CoolProvider
that is suppose to be exposed on all wrapped child components. A child component is wrapped using withCool()
such that it can access props.cool
. (Of course in a real world example everything is much more complex and there are dozens of components.)
Now independent of knowing whether a child component has been wrapped I would like to be able to get a reference to it via ref()
as illustrated in RootComponent
but unfortunately the wrapped components do not support ref()
anymore as they are functional!
Check out this live demo (https://jsfiddle.net/64t0oenz/3/) and inspect the web console. You will see that only the callback for the non-wrapped component fires.
My question is: is there a way to forward the reference request from the CoolConsumer
to the actual component such that the parent component can access it's reference?
const { Provider: CoolProvider, Consumer: CoolConsumer } = React.createContext();
const withCool = Component => props => (
<CoolConsumer>
{cool => <Component {...props} cool={cool} />}
</CoolConsumer>
);
class ChildComponent extends React.Component {
render() {
return this.props.cool ? (
<div>Isn't this cool?</div>
) : (
<div>Not so cool!</div>
);
}
}
const CoolChildComponent = withCool(ChildComponent);
class RootComponent extends React.Component {
render() {
return (
<CoolProvider value={true}>
<ChildComponent ref={(c) => { console.log('Normal child ref', c); }}/>
<CoolChildComponent ref={(c) => { console.log('Cool child ref', c); }}/>
</CoolProvider>
);
}
}
ReactDOM.render(<RootComponent />, document.querySelector('#cool'));
解决方案
我想到了!使用 React 的forwardRef
API 可以完全实现我想要的。唯一需要的改变是更换
const withCool = Component => props => (
<CoolConsumer>
{cool => <Component {...props} cool={cool} />}
</CoolConsumer>
);
和
const withCool = Component => React.forwardRef((props, ref) => (
<CoolConsumer>
{cool => <Component {...props} cool={cool} ref={ref} />}
</CoolConsumer>
));
这是修改后的现场演示:https ://jsfiddle.net/64t0oenz/4/ 打开开发控制台,您现在将看到 2 个控制台日志打印对正常和酷组件的引用。
推荐阅读
- java - 如何将 MySQL 数据库注入项目文件夹
- arrays - 如何在 swift 中一次在 Firestore 文档中设置多个字段
- java - 在 setOnEditCommit JavaFX 之后禁用 TableCell
- r - Jtools-错误消息-没有适用于“summ”的方法应用于类“list”的对象
- android - Android 自定义 SeekBar - 进度不跟随拇指
- java - 如何使用 Gradle 安装 Aspose.Words for Java
- java - javac:没有源文件
- r - 如何将具有相似变量名称的多个数据框组合成一个数据框?
- php - Carbon:在 Carbon::parse 中允许时间戳
- c# - 将 ComboBox 与整数集合绑定