react-component / form

React High Order Form Component(web & react-native)
http://react-component.github.io/form/
MIT License
1.81k stars 296 forks source link

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

Open yoyo837 opened 5 years ago

yoyo837 commented 5 years ago

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

deebov commented 5 years ago

the same problem

f97-2308 commented 5 years ago

Me too..!

DemetriusHR commented 5 years ago

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

ddf2008 commented 5 years ago

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

Seems the example is not for connect component

Liruimin2 commented 5 years ago

how to save

webwyb commented 4 years ago

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

TaurusWood commented 4 years ago

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

michaelckelly commented 4 years ago

@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)