【React】报错:React.createElement: type is invalid -- expected a string (for built-in components) or a c

技术栈

React / Ant Design Pro

背景

第一次使用antd pro 开发, 需要自定义一个全局业务组件,

问题

照着原项目在src/components 下创建个组件 , export default ComponentName , 然后再pages里引入 import { ComponentName } from ‘components’ , 于是报错 Warning: React.createElement: type is invalid – expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it’s defined in, or you might have mixed up default and named imports.
在这里插入图片描述
查了一下这是ES6 模块导出及引入的问题 , 仔细检查对比了其他业务组件的导出和引入, 都是这样做的, 实在想不通~

看到这个帖子 : https://www.cnblogs.com/wind-wang/p/9453167.html
终于解决了!!! 感谢原博!!

解决办法

组件 :


import React from 'react'

const TextEditor = ({ data }) => {
  return (
    <div>
    	我是个组件~
    </div>
  )
}

export default TextEditor

页面引入组件 :

import ComponentName from  'components/TextEditor';

说在最后
至今没想通为什么按照原项目的方式导出引入报错 (枯了…)

因为我在pages components 下同样方式也没问题呀~~~~~~~~

哎, 要好好补习ES6了 (哭哭哭~~~~)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值