简体   繁体   中英

TypeError: store.getState is not a function. (In 'store.getState()', 'store.getState' is undefined

I've been trying to integrate Redux into a project.

I followed the usage example, but I get the error store.getState is not a function.

So I know others have asked similar questions but the cases are a little different.

Red Code

Environment

OS: macOS Mojave 10.14
Node: 8.11.3
Yarn: 1.10.1
npm: 6.4.1
Watchman: 4.9.0
Xcode: Xcode 10.1

react-native-cli: 2.0.1 react-native: 0.57.2

Index.js

import React, {Component} from 'react';
import { AppRegistry } from 'react-native';

import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import promiseMiddleware from 'redux-promise';

import reducers from './src/Stores/reducers';

import App from './App';
import { name as appName } from './app.json';

const createStoreWithMiddleware = applyMiddleware(promiseMiddleware)(createStore)

const appRedux = () => (
    <Provider store={createStoreWithMiddleware}>
        <App />
    </Provider>
)

AppRegistry.registerComponent(appName, () => appRedux);

App.js

export default class App extends Component {
  render() {
    return (
      <View style={styles.container}>
     <Cards />
     </View>
    );
  }
}

Card_reducer.js

export default function(state={}, action){
    switch(action.type){
        case 'GET_ARTICLES':
        return{...state, cards:action.payload}
        default:
        return state;
    }
}

card > index.js

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
import { connect } from 'react-redux';
import { getCards } from '../Stores/actions'
import { bindActionCreators } from 'redux';

class Cards extends Component{

componentDidMount(){
    this.props.getCards()
}

    render(){
        return(
            <Text>Cartes</Text>
        )
    }
}

function mapStateToProps(state){
    console.log(state);
    return{
        cards: state.cards
    }
}

function mapDispatchToProps(dispatch){
    return bindActionCreators({getCards}, dispatch);
}

export default connect(mapStateToProps,mapDispatchToProps)(Cards);

Reducer > Index.js

import { combineReducers } from 'redux';

import cards from './card_reducer'

const rootReducer = combineReducers({
    cards
});

export default rootReducer;

Thank you for your help.

I forget to use reducers on appRedux :/

const appRedux = () => (
    <Provider store={createStoreWithMiddleware}>
        <App />
    </Provider>
)

become

const appRedux = () => (
    <Provider store={createStoreWithMiddleware(reducers)}>
        <App />
    </Provider>
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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