简体   繁体   English

如何让反应姿势与类组件一起工作?

[英]How to get react-pose working with class components?

I've followed the documentation and this blog post but I'm struggling to get anything to work.我已经关注了文档这篇博客文章,但我正在努力让任何事情发挥作用。

Locally, I get the following error: 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.在本地,我收到以下错误: 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. ,听着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. 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.

So I've attempted to forward the ref:所以我试图转发参考:

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} />
));

Which is working as I'm able to console.log the ref inside my parent Component:这是因为我能够在我的父组件中对ref进行console.log

ColorCheckbox {props: Object, context: Object, refs: Object, updater: Object, setRef: function ()…} "ref"

However, I still receive the message (above) of No valid DOM ref found... .但是,我仍然收到No valid DOM ref found...的消息(以上)。

Here's a simple Codesandbox describing my issue .这是一个简单的 Codesandbox 描述我的问题

About the Codesandbox:关于代码沙盒:

I am getting cross-origin errors in this Sandbox (they do not occur locally).我在这个沙箱中遇到跨域错误(它们不在本地发生)。 If you change line 14 to be ColorCheckbox the cross-origin error goes...如果您将第 14 行更改为ColorCheckbox则跨域错误会出现...

Any ideas?有任何想法吗?

When you call forwardRef on a class based component and try to pass the ref through the ref attribute it will not work.当您在基于类的组件上调用 forwardRef 并尝试通过 ref 属性传递 ref 时,它将不起作用。 The documentation example will only work for regular DOM elements.文档示例仅适用于常规 DOM 元素。 Rather try doing the following:而是尝试执行以下操作:

export default forwardRef((props, innerRef) => (
  <ColorCheckbox forwardRef={innerRef} {...props} />
));

I've just used an arbitrary name, so in this case forwardRef, to pass the ref as a prop.我刚刚使用了一个任意名称,因此在本例中为 forwardRef,将 ref 作为 prop 传递。 In your class based component I've changed the part where the ref is set on the button to this:在基于类的组件中,我已将按钮上设置 ref 的部分更改为:

const { key, children, selected, color, forwardRef } = this.props;
return (
  <button
    ref={forwardRef}
    key={key}
    style={{
    ...

The following approach, which they feature in their blog post, will only work for regular DOM elements and styled-components:他们在博客文章中介绍的以下方法仅适用于常规 DOM 元素和样式组件:

const MyComponent = forwardRef((props, ref) => (
  <div ref={ref} {...props} />
));

Please refer to my Codesandbox fork to see a working example.请参阅我的Codesandbox fork以查看工作示例。

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

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