简体   繁体   English

使无状态组件对类组件语法做出反应

[英]react stateless component to class component syntax

I just upgraded react from v15.6.2 to v 16.5.2 and now I am getting a few errors along the lines of... 我刚刚将反应从v15.6.2升级到v 16.5.2,现在我遇到了一些错误...

Warning: Stateless function components cannot be given refs. 警告:无状态功能组件不能提供引用。 Attempts to access this ref will fail. 尝试访问此引用将失败。

After searching for an answer, it looks like I need to convert some stateless components to class components. 在寻找答案之后,看来我需要将一些无状态组件转换为类组件。 My problem is with the syntax. 我的问题是语法。 If I have the following component, how would I convert it to a class component? 如果我有以下组件,该如何将其转换为类组件?

const CategoryForm = Form.create()(
  (props) => {
    const { visible, onCancel, onSubmit, form } = props;
    const { getFieldDecorator } = form;
    return (
      <Modal
        visible={visible}
        title="Create New Category"
        okText="Create"
        onCancel={onCancel}
        onOk={onSubmit}
      >
        {this.renderForm(getFieldDecorator)}
      </Modal>
    );
  }
);
const CategoryForm = Form.create()(class extends React.Component {
  render() {
    const { visible, onCancel, onSubmit, form } = this.props;
    const { getFieldDecorator } = form;

    return (
      <Modal
        visible={visible}
        title="Create New Category"
        okText="Create"
        onCancel={onCancel}
        onOk={onSubmit}
      >
        {this.renderForm(getFieldDecorator)}
      </Modal>
    );
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM