我已经按照文档和这篇博客文章进行了操作,但我正在努力让任何工作正常进行。
在本地,我收到以下错误: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...
.
关于 Codesandbox:
我在此沙盒中遇到跨域错误(它们不会在本地发生)。如果您将第 14 行更改ColorCheckbox
为跨域错误...
有任何想法吗?