将 Redux 添加到 React-Native 应用程序?

Posted

技术标签:

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

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

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

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

存储\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 输出之外,您能否包括完整的堆栈跟踪? 【参考方案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 调用。我一直在教程之间跳来跳去,因为我找不到一个容易理解的教程。

以上是关于将 Redux 添加到 React-Native 应用程序?的主要内容,如果未能解决你的问题,请参考以下文章

React-Native Redux reducer switch 语句忽略类型

使用 react-redux 与 react-native 连接时的不变违规

最易懂的 redux (react-native state管理神器)

我无法在带有 redux 的 react-native 中使用地理定位

React-Native项目为什么要使用redux

React-native + Redux(Combine Reducers):创建状态结构