form icon indicating copy to clipboard operation
form copied to clipboard

Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

Open yoyo837 opened this issue 5 years ago • 8 comments

Item.js:


@connect() // react-redux ConnectFunction
class Item extends Component { // Custom Component
  // ...
}

export default Item;

use:

<Form.Item label="开始日期">
  {form.getFieldDecorator('startDate', {
    rules: [
      {
        required: true,
        message: '请选择开始日期',
      },
    ],
  })(
    <Item // Custom Component
      {...some props}
    />
  )}
</Form.Item>

If the getFieldDecorator receives a @connect connected component, you will get the warning.

A temporary solution is:

Item.js

// ...
// export default Item;

// eslint-disable-next-line react/no-multi-comp
export default class ItemWrapper extends Component {
  render() {
    const {children, ...props} = this.props
    return <Item {...props}>{children}</Item>
  }
}

related: ant-design/ant-design/issues/11324

yoyo837 avatar Apr 26 '19 11:04 yoyo837

the same problem

deebov avatar Apr 29 '19 10:04 deebov

Me too..!

f97-2308 avatar Jul 02 '19 09:07 f97-2308

In the doc of the Ant, is recommended use ForwardRef of the React.

Doc Ant: https://ant.design/components/form/#components-form-demo-customized-form-controls Example: https://codesandbox.io/s/antd-reproduction-template-q4bdi

DemetriusHR avatar Jul 02 '19 11:07 DemetriusHR

https://codesandbox.io/s/antd-reproduction-template-q4bdi

Seems the example is not for connect component

ddf2008 avatar Nov 06 '19 00:11 ddf2008

how to save

Liruimin2 avatar Nov 10 '19 05:11 Liruimin2

尝试下: @connect(mapStateToProps, null, null, { forwardRef: true })

webwyb avatar Jan 02 '20 15:01 webwyb

@webwyb 为什么需要ref才不会报错啊,现在connect是函数组件了么?

TaurusWood avatar Jan 14 '20 02:01 TaurusWood

@connect(mapStateToProps, null, null, { forwardRef: true })

Thanks @webwyb , this worked for me if anyone is in a similar boat my code ended up as:

export default compose(
  Form.create({ name: 'formName' }),
  connect(mapStateToProps, { actionCreators }, null, { forwardRef: true })
)(ComponentName)

michaelckelly avatar Mar 06 '20 19:03 michaelckelly