【问题标题】:Adding Redux to React-Native App?将 Redux 添加到 React-Native 应用程序?
【发布时间】:2018-07-21 07:17:00
【问题描述】:

我正在尝试将 Redux 添加到 React Native 应用程序并收到以下错误:

预计 reducer 是一个函数。 创建商店

我已经对此进行了 4 天的调试,但我看不出我的代码与迄今为止我看过的任何教程或 stackoverflow 问题有什么不同。任何帮助将不胜感激,因为我完全陷入困境。

存储\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;
    }
}

【问题讨论】:

  • 除了必须出现在某处的Expected the reducer to be a function. createStore 输出之外,您能否包括完整的堆栈跟踪?

标签: javascript react-native redux redux-thunk


【解决方案1】:

根据您的store\index.js 看来您忘记从redux 导入applyMiddleware

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;

【讨论】:

  • 添加了导入语句,但我仍然遇到同样的错误。感谢您的回复。
  • 我发现了错误,我在 App.js 中有另一个 createStore 调用。我一直在教程之间跳来跳去,因为我找不到一个容易理解的教程。
猜你喜欢
  • 2017-10-24
  • 1970-01-01
  • 1970-01-01
  • 2016-08-06
  • 1970-01-01
  • 2016-03-23
  • 2019-11-05
  • 1970-01-01
  • 2019-06-18
相关资源
最近更新 更多