【发布时间】:2019-04-10 20:56:07
【问题描述】:
我关注了the documentation 和this blog post,但我一直在努力让任何事情发挥作用。
在本地,我收到以下错误:HEY, LISTEN! No valid DOM ref found. If you're converting an existing component via posed(Component), you must ensure you're passing the ref to the host DOM node via the React.forwardRef function.
所以我尝试转发参考:
class ColorCheckbox extends Component {
setRef = ref => (this.ref = ref);
constructor(props) {
super(props);
}
render() {
const { key, children, color } = this.props;
return (
<button
ref={this.setRef}
key={key}
style={{
...style.box,
background: color,
}}
>
{children}
</button>
);
}
}
export default forwardRef((props, innerRef) => (
<ColorCheckbox ref={innerRef} {...props} />
));
我可以在我的父组件中 console.log ref 工作:
ColorCheckbox {props: Object, context: Object, refs: Object, updater: Object, setRef: function ()…}
"ref"
但是,我仍然收到No valid DOM ref found... 的消息(上图)。
Here's a simple Codesandbox describing my issue.
关于 Codesandbox:
我在此沙盒中遇到跨域错误(它们不会在本地发生)。如果您将第 14 行更改为 ColorCheckbox,则会出现跨域错误...
有什么想法吗?
【问题讨论】:
标签: javascript reactjs animation react-pose