【问题标题】:Using React Native with Redux在 Redux 中使用 React Native
【发布时间】: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);

我该如何解决这个问题?我是否正确地诊断了这一点?

【问题讨论】:

  • 组件是如何导入的?
  • {() =&gt; &lt;App /&gt;} 只是一个匿名函数声明,如果要渲染组件,请将其包装在 IIFE 中:{(() =&gt; &lt;App /&gt;)()}。当然,你应该避免引入这种不必要的复杂性,所以在这里写&lt;App /&gt;
  • @Igorsvee 这就是我最初尝试的方法 - 你指的是我在其他地方读到的另一种尝试。
  • 你能在你定义store的地方添加代码吗?很可能是 Provider 组件或它尝试使用的 store 变量引发了错误。

标签: javascript reactjs react-native redux react-redux


【解决方案1】:

当您看到消息“期望字符串(用于内置组件)或类/函数”时。实际上,因为您没有在 action 或 reducer 中导出函数。请仔细检查。

加油!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 2017-02-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多