【问题标题】:You likely forgot to export your component from the file it's defined in您可能忘记从定义的文件中导出组件
【发布时间】:2019-10-30 15:15:37
【问题描述】:

我启动组件 mount() 工作正常,但我按 F5 响应该错误。

/* eslint-disable react/prop-types */
/* eslint-disable react/destructuring-assignment */
import React from 'react';
import {
  Col, Row, Input, Select, Upload, Icon, Form, Button,
} from 'antd';
import 'react-quill/dist/quill.snow.css';
import 'react-quill/dist/quill.core.css';


const { Option } = Select;

const NewPost = Form.create({ name: 'newPost' })(
  class extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        loading: false,
        children: [],
      };
      if (typeof window !== 'undefined') {
        // eslint-disable-next-line global-require
        this.quill = require('react-quill');
      }
    }

    handleSubmit = (e) => {
      e.preventDefault();
      this.props.form.validateFieldsAndScroll((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values);
        }
      });
    };

    render() {
      const Quill = this.quill;
      console.log(Quill);
      const { props } = this;
      const { getFieldDecorator } = props.form;
      const imageUrl = '';
      const { loading, children } = this.state;
      const uploadButton = (
        <Row>
          <Icon type={loading ? 'loading' : 'plus'} />
          <Row className="ant-upload-text">Upload</Row>
        </Row>
      );
      return (
        <Form onSubmit={this.handleSubmit}>
          <Quill /> // error in here
          <Col span={20} offset={2}>
            <Row>
              <Button type="primary" className="text-right">Save</Button>
            </Row>
            <hr />
            <br />
            <Row>
              <Col span={17}>
                <Row>
                  <Col span={6}>Title</Col>
                  <Col span={18}>
                    <Form.Item>
                      {getFieldDecorator('title', {})(<Input />)}
                    </Form.Item>
                  </Col>
                </Row>
                <br />
                <Row>
                  <Col span={6}>Abstract</Col>
                  <Col span={18}>
                    <Form.Item>
                      {getFieldDecorator('abstract', {})(<Input.TextArea rows={6} />)}
                    </Form.Item>
                  </Col>
                </Row>
                <br />
                <Row>
                  <Col span={6}>Category</Col>
                  <Col span={18}>
                    <Form.Item>
                      <Select defaultValue="combobox" style={{ width: '100%' }}>
                        <Option value="combobox">Combobox</Option>
                      </Select>
                    </Form.Item>
                  </Col>
                </Row>
                <br />
                <Row>
                  <Col span={6}>Content</Col>
                  <Col span={18}>
                    <Form.Item>
                      {getFieldDecorator('content', {})(
                        <Input.TextArea rows={8} />,
                      )}
                    </Form.Item>
                  </Col>
                </Row>
              </Col>
              <Col span={6} offset={1}>
                <Row>
                  <Col>Status</Col>
                </Row>
                <br />
                <Row>
                  <Select defaultValue="public" style={{ width: '100%' }}>
                    <Option value="public">Public</Option>
                  </Select>
                </Row>
                <br />
                <Row>
                  <Col span={10}>Author</Col>
                  <Col>
                    <a href="#">Administrator</a>
                  </Col>
                </Row>
                <br />
                <Row>
                  <Col span={10}>Create at</Col>
                  <Col>
                    <a href="#">10/10/2019</a>
                  </Col>
                </Row>
                <br />
                <Row>
                  <Col span={10}>Last modified</Col>
                  <Col>
                    <a href="#">10/10/2019</a>
                  </Col>
                </Row>
                <br />
                <Row>
                  <Col>Tags</Col>
                </Row>
                <br />
                <Row>
                  <Col>
                    <Select mode="tags" style={{ width: '100%' }} placeholder="Tags">
                      {children}
                    </Select>
                  </Col>
                </Row>
                <br />
                <Row>
                  <Col>Thumbnail</Col>
                </Row>
                <br />
                <Row>
                  <Upload
                    listType="picture-card"
                    className="avatar-uploader"
                    showUploadList={false}
                    action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
                    onChange={this.handleChange}
                  >
                    {imageUrl ? <img src={imageUrl} name="imageUrl" alt="avatar" /> : uploadButton}
                  </Upload>
                </Row>
                <br />
              </Col>
            </Row>
          </Col>
          <Col className="clear" />
          <style>
            {`
              .avatar-uploader .ant-upload,
              .avatar-uploader img {
                width: 100%;
              }
              hr{
                border: none;
                border-bottom: 1px solid rgb(235, 237, 240);
              }
              .text-right{
                float: right;
              }
              .clear{
                clear: both;
              }
            `}
          </style>
        </Form>
      );
    }
  },
);

export default NewPost;

得到以下错误: 元素类型无效:应为字符串(用于内置组件)或类/函数(用于复合组件)但得到:未定义。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。

【问题讨论】:

  • 您是如何导入 NewPost 的?检查其他组件是否以正确的路径正确导入

标签: javascript reactjs


【解决方案1】:

您需要将其导入为

从“NewPost 组件的路径”导入 NewPost

例如:从“./NewPost”导入NewPost

并确保相对路径正确。

react-quill 的固定工作示例: https://codesandbox.io/s/named-import-wufq9

【讨论】:

  • 但我不知道我的代码和你的代码有什么区别 :)),因为我引入了 NewPost 路径。这是我的 index.js 文件codesandbox.io/s/relaxed-benz-ne4ft
  • 当然,我确定相对​​我的路径是正确的,因为当我不挂载 组件时它正在工作,但在挂载 之后它会响应该错误
  • 根据 react-quill 文档尝试通过 import ReactQuill, { Quill, Mixin, Toolbar } from 'react-quill'; 导入 quill 参考这里:github.com/zenoamaro/react-quill#use-the-component
  • @QuangDuy 更新了 ReactQuill 及其工作的沙箱示例。请检查
  • 如果上述解决方案有效,请随时为答案投票。 :)
猜你喜欢
  • 2019-06-02
  • 1970-01-01
  • 2020-08-13
  • 2019-11-29
  • 2020-12-08
  • 2021-01-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
相关资源
最近更新 更多