【React】【Antd】警告报错:Warning: Function components cannot be given refs. Attempts to access this ref wil

技术栈

React / Ant Design Pro / Ant Design

警告报错

Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
在这里插入图片描述

原因

自定义组件嵌套在Antd Form中 , 没有使用React.forwardRef包裹

		<Form>
          <FormItem label="图文详情" {
    
    ...formItemLayout}>
            {
    
    getFieldDecorator('resourceHtml', {
    
    
              initialValue: formDataList.resourceHtml || '',
              rules: [
                {
    
    
                  required: true,
                  message: '必选项',
                },
              ],
            })(<TextEditor onChangeText={
    
    onChangeText} />)}
          </FormItem>
         <Form />

在这里插入图片描述
以上详细原因截图来源 原博 : https://blog.csdn.net/weixin_44216157/article/details/124027743

解决

在自定义子组件上包裹一层React.forwardRef

export default Form.create()(React.forwardRef(TextEditor))

猜你喜欢

转载自blog.csdn.net/qq_45481971/article/details/129835307