import {
Form,
Input,
Tooltip,
Icon,
Cascader,
Select,
Row,
Col,
Checkbox,
Button,
} from 'antd';
function BaseForm({form}) {
const { getFieldDecorator } = form;
onSubmit = e => {
e.preventDefault();
form.validateFieldsAndScroll((err, values) => {
if (!err) {
console.log('Received values of form: ', values);
}
});
};
return (
<Form onSubmit={onSubmit}>
<Form.Item label="Password" hasFeedback>
{getFieldDecorator('password', {
rules: [
{
required: true,
message: 'Please input your password!',
},
],
})(<Input.Password />)}
</Form.Item>
</Form>
)
}
const EnhanceForm = React.forwardRef((props, _ref) => {
return Form.create()(BaseForm)
}
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 包裹函数组件
import React, { forwardRef } from 'react';
const EnhanceForm = forwardRef((props, _ref) => {
return <div> todo </div>
}
export default EnhanceForm;
react组件中使用了 connect连接redux后的组件,然后其他地方引用该组件后就提示以上警告,不影响使用
export default connect(stateToProps, null, null, { forwardRef: true })(BaseForm);