TypeError:未定义不是对象(正在评估“ store.getState”)

时间:2018-12-12 07:23:37

标签: javascript react-native ecmascript-6 redux

我正在关注Let’s Build: Cryptocurrency Native Mobile App With React Native + Redux教程。

当我在App.js中创建商店时,该应用程序运行正常

import { createStore, applyMiddleware, compose } from 'redux';
import devTools from 'remote-redux-devtools';
import React, { Component } from 'react';
import { Platform, View } from 'react-native';
import { Provider } from 'react-redux';
import promise from 'redux-promise';
import thunk from 'redux-thunk';
import logger from 'redux-logger';

import { Header, CryptoContainer } from './src/components';
import rootReducer from './src/reducers';    

const middleware = applyMiddleware(thunk, promise, logger);

const Store = createStore(rootReducer, compose(middleware, devTools({
  name: Platform.OS,
  hostname: 'localhost',
  port: 5678
}), ));

export default class App extends Component {
  render() {
    return (
      <Provider store={Store}>
        <View>
          <Header />
          <CryptoContainer />
        </View>
      </Provider>
    );
  }
}

但是当我将存储逻辑移动到新文件./src/Store.js时,

import { Platform } from 'react-native';    
import { createStore, applyMiddleware, compose } from 'redux';
import devTools from 'remote-redux-devtools';
import promise from 'redux-promise';
import thunk from 'redux-thunk';
import logger from 'redux-logger';

import rootReducer from './reducers';

const middleware = applyMiddleware(thunk, promise, logger);

const Store = createStore(rootReducer,compose(middleware,devTools({
            name: Platform.OS,
            hostname: 'localhost',
            port: 5678
        }),
    )
);

export default Store;

并像

一样在App.js中使用
import React, { Component } from 'react';
import { View } from 'react-native';
import { Provider } from 'react-redux';

import { Header, CryptoContainer } from './src/components';
import { Store } from './src/Store';

export default class App extends Component {
  render() {
    return (
      <Provider store={Store}>
        <View>
          <Header />
          <CryptoContainer />
        </View>
      </Provider>
    );
  }
}

我知道

  

TypeError:未定义不是对象(正在评估“ store.getState”)

是什么导致我导入expo start时构建(Store.js)失败?

4 个答案:

答案 0 :(得分:2)

似乎import语句不正确。应该是:

import Store from './src/Store';

答案 1 :(得分:1)

Itunu Adekoya的回答表明,您可以决定如何导出/导入,在这种情况下,可以选择个人喜好,因为两者之间没有明显的差异。

在一个文件中有很多导出的情况下,也许有些不相关或不会一起使用,最好将它们导出为const,然后在其他文件中仅导入所需的内容通过import {}格式,可以确保只包含相关的提示

答案 2 :(得分:0)

如果要导入single named export
例如,您完成export const MyComponent = () => {}的位置就像
import { MyComponent } from "./MyComponent"

一样导入

如果您要导入default export
,例如完成const MyComponent = () => {} export default MyComponent的位置,则可以像
import MyDefaultComponent from "./MyDefaultExport"

那样导入它

答案 3 :(得分:0)

我收到此错误是因为我从我的主App文件中导出了错误的组件。

我正在导出此文件:

import React from 'react'
import { Provider } from 'react-redux'
import { createAppContainer } from 'react-navigation'
import Navigator from './src/components/Navigator'
import { store } from './src/store'

const App = createAppContainer(Navigator);

const Wrapped = props => (
  <Provider store={store}>
    <App />
  </Provider>
)

export default Provider; // wrong!

最后一行应该是:

export default Wrapped; // right!