1. import {
  2. Form,
  3. Input,
  4. Tooltip,
  5. Icon,
  6. Cascader,
  7. Select,
  8. Row,
  9. Col,
  10. Checkbox,
  11. Button,
  12. } from 'antd';
  13. function BaseForm({form}) {
  14. const { getFieldDecorator } = form;
  15. onSubmit = e => {
  16. e.preventDefault();
  17. form.validateFieldsAndScroll((err, values) => {
  18. if (!err) {
  19. console.log('Received values of form: ', values);
  20. }
  21. });
  22. };
  23. return (
  24. <Form onSubmit={onSubmit}>
  25. <Form.Item label="Password" hasFeedback>
  26. {getFieldDecorator('password', {
  27. rules: [
  28. {
  29. required: true,
  30. message: 'Please input your password!',
  31. },
  32. ],
  33. })(<Input.Password />)}
  34. </Form.Item>
  35. </Form>
  36. )
  37. }
  38. const EnhanceForm = React.forwardRef((props, _ref) => {
  39. return Form.create()(BaseForm)
  40. }
  41. export default EnhanceForm;

Function components cannot be given refs

Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
在Antd Form 组件中使用自定义 Function component时提示以上警告错误
解决:
使用 forwardRef 包裹函数组件

  1. import React, { forwardRef } from 'react';
  2. const EnhanceForm = forwardRef((props, _ref) => {
  3. return <div> todo </div>
  4. }
  5. export default EnhanceForm;

react组件中使用了 connect连接redux后的组件,然后其他地方引用该组件后就提示以上警告,不影响使用

  1. export default connect(stateToProps, null, null, { forwardRef: true })(BaseForm);