简体   繁体   中英

Adding Redux to React-Native App?

I'm attempting to add Redux a React Native app and receive the following error:

Expected the reducer to be a function. createStore

I've been debugging this for 4 days and I can't see what is different from my code than any of the tutorial or stackoverflow questions I've looked at so far. Any help would be greatly appreciated as I'm completely stuck.

store\\index.js

import { createStore } from 'redux';
import rootReducer from '../redux/reducers/index';
import thunk from 'redux-thunk';

let store = createStore(
    rootReducer,
    applyMiddleware(thunk)
);

export default store;

redux\\reducers\\index.js

import { combineReducers } from 'redux';
import users_reducer from './users_reducer';

const rootReducer = combineReducers({
    users_reducer
});

export default rootReducer;

redux\\reducers\\users_reducer.js

import * as types from '../actions/users_action';
const defaultState = {

    isAuthenticated: false,
    isFetchingIsRegistered: false,
    isRegistered: false,
    email: '',
    firstname: '',
    lastname: '',
    imageUrl: '',
    defaultSport: 0,
    sports: [],
    authenticationToken: '',
    refreshToken: ''
}

export default function reducer(state = defaultState, action) {
    switch (action.type) {
        case 'REGISTERUSER':
            return Object.assign({}, state, {
                isRegistered: true,
                email: action.email,
                firstname: action.firstname,
                lastname: action.lastname,
                imageUrl: action.imageUrl,
                authenticationToken: action.authenticationToken,
                refreshToken: action.refreshToken
            });
        case 'AUTHENTICATUSER':
            return Object.assign({}, state, {
                isAuthenticated: action.isAuthenticated
            });
        case 'REQUEST_ISREGISTERED':
            return Object.assign({}, state, {
                isFetchingIsRegistered: true
            });
        case 'RECEIVE_ISREGISTERED':
            return Object.assign({}, state, {
                isFetchingIsRegistered: false,
                isRegistered: true
            });

        case types.ISREGISTERED_SUCCESS:
            return action.isRegistered
        default:
            return state;
    }
}

According to your store\\index.js seems that your forgot to import applyMiddleware from redux :

import { createStore, applyMiddleware } from 'redux';
import rootReducer from '../redux/reducers/index';
import thunk from 'redux-thunk';

let store = createStore(
    rootReducer,
    applyMiddleware(thunk)
);

export default store;

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