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. Check the render method of `Add`. in AdvertisementAdd (created by Connect(AdvertisementAdd)) in Connect(AdvertisementAdd) (created by Route) in Route (created by Full) in Switch (created by Full) in div (created by Container) in Container (created by Full) in main (created by Full) in div (created by Full) in div (created by Full) in Full (created by Route) in Route in Switch in Router (created by ConnectedRouter) in ConnectedRouter in Provider
问题背景
使用React 15.6.1 + Webpack 3.6.0,导入一个组件
import { Select,Option, OptGroup } from '../../component/Tree';
结果控制台报错 ,错误信息信息如下所示:
发现这种错误其实很无奈,因为很隐蔽,没有指向具体的错误代码,不太容易定位到具体代码位置。 然而经过 Google
一番搜索,在 Stack Overflow
找到个说法。
解决办法
此处原因是因为 没有导出Option
和OptGroup
,这两个组件是在Select
组件下引出的。 要么直接 export const Option
和 export const OptGroup
; 要么在Select导出: const { Option, OptGroup} = Select;
;
上面的解决方法只是思路。遇到同样问题时,应该查找导入的类是否有问题
,从而可以迅速的找到根源,同时也希望遇到问题的小伙伴能常去Stack Overflow
摸索,各种各样的奇葩问题都能在这找到,就算找不到你想要的问题,也能有一些案例能够使你领悟。