【发布时间】:2017-02-16 12:25:42
【问题描述】:
所以我正在尝试使用 react 进行 redux,我在模拟器中不断收到此错误
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
和
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.
ExceptionsManager.js:63Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
我已经把问题归结为这个
<Provider store={store}>
<App />
</Provider>
当我删除提供程序时,App 呈现的内容(假设它们是一个简单的“Hello World”文本组件)。当我把它放回去时,它没有。
我知道在旧版本的 react 中这是一个问题,但我在 15.4.0。
但以防万一,我尝试做这样的事情
// wrapper.js
const wrapper = () => {
return (
<Provider store={store}>
{() => <App />}
</Provider>
);
}
// index
AppRegistry.registerComponent('BasketballAndroidFrontend', () => wrapper);
我该如何解决这个问题?我是否正确地诊断了这一点?
【问题讨论】:
-
组件是如何导入的?
-
{() => <App />}只是一个匿名函数声明,如果要渲染组件,请将其包装在 IIFE 中:{(() => <App />)()}。当然,你应该避免引入这种不必要的复杂性,所以在这里写<App /> -
@Igorsvee 这就是我最初尝试的方法 - 你指的是我在其他地方读到的另一种尝试。
-
你能在你定义
store的地方添加代码吗?很可能是 Provider 组件或它尝试使用的 store 变量引发了错误。
标签: javascript reactjs react-native redux react-redux