添加Redux以响应本机?

时间:2019-08-11 12:04:30

标签: reactjs react-native react-redux

我目前是第一次使用react-native开发应用程序, 但我正在努力添加redux。 我收到此错误TypeError: Cannot read property 'getState' of undefined,但我不知道如何解决。 这是我的代码:

import React from "react";
import Home from "./home";


import { store } from "./redux/store";
import { Provider } from "react-redux";
/* @flow */

import { View, StyleSheet } from "react-native";

class App extends React.Component {
  render() {
    return (
      <Provider store={store}>
        <View style={styles.container}>
          <Home />
        </View>
      </Provider>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1
  }
});

store.js:

import { applyMiddleware, createStore } from "redux";
import logger from "redux-logger";

import reducer from "./reducer";

const middlewares = [logger];

const store = createStore(reducer, applyMiddleware(...middlewares));

export default store;

reducer.js:

import { combineReducers } from "redux";

import mapReducer from "../redux/maps/maps-reducer";


export default combineReducers({
  map: mapReducer
});

maps-action.js:

import MAPSActionTypes from './maps-action-types';

export const currentlocation = () => ({
    console.log(${location});
    type : MAPSActionTypes.GET_CURRENT_LOCATION
});

maps-reducer.js:

import MAPSActionTypes from "./mapsactiontypes";

const INITIAL_STATE = {
  location: {}
};

const mapReducer = (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case MAPSActionTypes.GET_CURRENT_LOCATION:
      return {
        ...state,
        location: action.payload
      };
    default:
      return state;
  }
}

export default mapReducer;

home.js:

import React from 'react';
import {
  StyleSheet,
  View,
  Text,
} from 'react-native';



import {connect} from 'react-redux'


const Home = (props) => {

  return (
      <View style={styles.container}>
        <Text>welcome</Text>
        <Text>{props.location}</Text>
      </View>
  );
};

const styles = StyleSheet.create({
  container:{
    flex:1,
    justifyContent:'center',
    alignItems:'center'
  }
});


const mapStateToProps = (state) => {
  return {
    location: state.map.location
  }
}

export default connect(mapStateToProps)(Home);

我需要更多的澄清或更多的细节。 如果有人可以帮助我解决该问题,我将非常感激。

1 个答案:

答案 0 :(得分:0)

问题是您export defaultstore.js文件中。

export default store;

但是在App中,您用import {}。因此,您导入的storeundefined

import { store } from "./redux/store";

正确的导入是

import store from "./redux/store";