简体   繁体   English

TypeError: undefined is not an object (evaluating 'store.getState')

[英]TypeError: undefined is not an object (evaluating 'store.getState')

I'm following the Let's Build: Cryptocurrency Native Mobile App With React Native + Redux tutorial.我正在关注Let's Build:Cryptocurrency Native Mobile App With React Native + Redux教程。

When I create my store in App.js , the app works fine当我在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>
    );
  }
}

but when I move the store logic to a new file ./src/Store.js ,但是当我将商店逻辑移动到新文件./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;

and use it in App.js like并在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>
    );
  }
}

I get我得到

TypeError: undefined is not an object (evaluating 'store.getState') TypeError: undefined is not an object (evaluating 'store.getState')

What's causing my build ( expo start ) to fail when I import Store.js ?当我导入Store.js时,是什么导致我的构建 ( expo start ) 失败?

It seems the import statement is not right.导入语句似乎不正确。 It should be:它应该是:

import Store from './src/Store';

if you're importing a single named export如果您要导入single named export
eg where you've done export const MyComponent = () => {} you'd import it like例如,你在哪里完成export const MyComponent = () => {}你会导入它
import { MyComponent } from "./MyComponent"

if you're importing a default export如果您要导入default export
eg where you've done const MyComponent = () => {} export default MyComponent you'd import it like例如,你在哪里完成了const MyComponent = () => {} export default MyComponent你会导入它
import MyDefaultComponent from "./MyDefaultExport"

I got this error because I was exporting the wrong component from my main App file.我收到此错误是因为我从我的主 App 文件中导出了错误的组件。

I was exporting this:我正在导出这个:

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!

That last line should be:最后一行应该是:

export default Wrapped; // right!

The answer from Itunu Adekoya shows that you can decide how you want to export / import, and in this case about personal preference, as there isn't a perf difference. Itunu Adekoya 的回答表明您可以决定导出/导入的方式,在这种情况下是关于个人偏好的,因为没有性能差异。

In the case where you have a lot of exports from a file, and perhaps some are unrelated or won't all be used together, it is better to export them individual as consts and then in other file only import what you need via import { } format, this will be sure to only include relevant imprts如果你从一个文件中导出了很多,并且可能有些是不相关的或者不会全部一起使用,最好将它们单独导出为常量,然后在其他文件中只导入你需要的内容通过 import { } 格式,这将确保只包含相关的 imprts

in my case its casing & named import issue.在我的例子中,它的外壳和命名的导入问题。 imported as导入为

import store from './Redux/Store'

it should be它应该是

import {Store} from './Redux/Store'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM