Javascript 传递到渲染属性时未定义对DOM实例的引用
我正在尝试使用一些嵌套的渲染道具来合成一个按钮,该按钮可以选择使用图标,而图标又可以选择使用工具提示,当鼠标悬停或单击图标时,工具提示将显示出来。为此,我想我需要将对Javascript 传递到渲染属性时未定义对DOM实例的引用,javascript,reactjs,functional-programming,jsx,Javascript,Reactjs,Functional Programming,Jsx,我正在尝试使用一些嵌套的渲染道具来合成一个按钮,该按钮可以选择使用图标,而图标又可以选择使用工具提示,当鼠标悬停或单击图标时,工具提示将显示出来。为此,我想我需要将对图标的引用向下传递到子工具提示组件 根据我所读到的,我正在做的 // Icon componentDidMount() { this.iconInstance = ReactDOM.findDOMNode(this); console.log('Icon mounted, instance', this.i
图标的引用向下传递到子工具提示组件
根据我所读到的,我正在做的
// Icon
componentDidMount() {
this.iconInstance = ReactDOM.findDOMNode(this);
console.log('Icon mounted, instance', this.iconInstance);
}
render() {
console.log('Icon render iconInstance', this.iconInstance);
return (
<span style={{ display: 'inline' }}>
<span>{this.props.iconText}</span>
{this.props.tooltip({ iconInstance: this.iconInstance })}
</span>
);
}
在我设置this.iconInstance
之后,可以立即将其记录下来,这样看起来就可以正常工作了。但是在渲染函数中,我没有定义
可能有一种方法可以使用forwardRef
来执行此操作,但是我找不到一种方法将Tooltip
组件作为渲染道具从App
组件向下传递到Icon
并向其添加ref
道具,例如,{this.props.Tooltip(this.icontance)}
收到一个错误,表示无法通过这样的引用,它将失败
ComponentDidMount是在第一次调用render方法后执行的方法。
这是在执行呈现方法之前调用的ComponentWillMount方法。这是您与上述人员讨论的内容。啊,那么这应该是上述讨论中的注释,而不是它自己的答案,但无论如何,这不是真正的问题。不过,谢谢。我们需要50分作为评论任何帖子的最低声誉分数,通过阅读我的答案,你投了反对票,并将我的分数降低了1。请使用演示问题的按钮更新你的问题,最好是使用堆栈片段([]
工具栏按钮)运行的问题。堆栈代码段支持React,包括JSX。
componentDidMount() {
console.log('Tooltip mounted', this.props);
// fails because iconInstance is undefined
// this.props.iconInstance.addEventListener(
// this.props.mouseEvent,
// this.showTooltip
// );
}