Redux - 应用程序状态将reducer的名称作为键

时间:2017-04-03 13:28:27

标签: javascript reactjs redux react-redux reducers

有人可以帮我解决这个问题吗? 我已经开始学习React和Redux,但是我在配置redux时遇到了几天。

我假设当某些东西触发一个动作时,通过reducers函数堆栈的redux应该返回一个代表我的应用程序状态的对象。 不幸的是,它返回一个{reducerName =>的对象reducer result}基本上意味着如果我有4个reducer,函数store.getState()返回类似

的东西
{
 'reducerOne': entireApplicationState
 'reducerTwo': entireApplicationState
 'reducerThree': entireApplicationState
 'reducerFour': entireApplicationState
}

如果有人可以帮助我,我会非常感激,因为我已经完成了所有的想法:)

这是我的 application.js

import React from 'react';
import ReactDom from 'react-dom';
import HomePage from 'root_views/home';
import {store} from 'root_services/redux/store';

class Application extends React.Component {

        constructor(props) {
            super(props);
        }

        render() {
            return (
                <HomePage/>
            )
        }

}

var Provider = React.createClass({
        childContextTypes: {
            store: React.PropTypes.object.isRequired
        },
        getChildContext: function () {
            return {store: this.props.store}
        },
        render: function () {
            return this.props.children;
        }
});


ReactDom.render(
        <Provider store={store}>
            <Application/>
        </Provider>,
        document.getElementById('application')
);

我的 store.js

    import { createStore } from 'redux';

    import {rootReducer} from './reducers/container';

    export const store = createStore(
        rootReducer,
        window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
    );

我的 container.js 基本上包含我的所有Reducer

import {combineReducers} from 'redux';

// This is just the action label
import {DATA_EXCHANGE_LOAD} from 'root_services/redux/actions/container'

const initialState = {
    data_exchange: {},
}

function dataExchange(state = {}, action) {

    switch (action.type) {

        case DATA_EXCHANGE_LOAD:
            return Object.assign({}, state, {
                data_exchange:{'reducerOne':'dataExchange'}
            });
            break;

        default:
            return initialState;
            break;

    }
};

function testReducer(state = {}, action) {
    switch (action.type) {

        case DATA_EXCHANGE_LOAD:
            return Object.assign({}, state, {
                data_exchange:{'reducerTwo':'testReducer'}
            });
            break;

        default:
            return initialState;
            break;
    }

};

// Export the combined reducers
export const rootReducer = combineReducers({
    dataExchange,
    testReducer
});

这是触发事件的操作:

export function dataExchangeLoad(){
    return {
        type: DATA_EXCHANGE_LOAD,
    }
};

这是触发操作的组件:

import React from 'react'
import "../components/layouts/header/header.less";
import {dataExchangeLoad} from "root_services/redux/actions/container"

export default class HomePage extends React.Component {

    constructor(props, {store}) {
        super(props);
        store.dispatch(dataExchangeLoad());
        console.log(store.getState());
    }

    render() {
        return (
            <div>
                <h1>test</h1>
            </div>
        )
    }
};

HomePage.contextTypes = {
    store: React.PropTypes.object,
}

结果如下:

Object {dataExchange: Object, testReducer: Object}

2 个答案:

答案 0 :(得分:3)

正如评论combineReducers中已经回答的那样,确实有效。如果您想链接reducer以便操作将遍历所有这些,可以使用reduce-reducers按顺序更新每个属性。使用这个辅助函数可以做类似的事情(看起来就像你想要实现的那样):

import reduceReducers from 'reduce-reducers';

const reducer1 = (state = {}, action) => { 
  if (action.type === 'foo') {
    return ({ 
      ...state, 
      touchedBy: ['reducer1'],
    })
  }
  return state;
};

const reducer2 = (state = {}, action) => { 
  if (action.type === 'foo') {
    return ({ 
      ...state, 
      touchedBy: state.touchedBy.concat('reducer2'),
    })
  }
  return state;
};

const reducer = reduceReducers(reducer1, reducer2);

expect(reducer({}, { type: 'foo' }))
    .toMatchObject({ touchedBy: ['reducer1', 'reducer2'] }); 

答案 1 :(得分:0)

万一有人在看,上面评论中提供的链接将被破坏。此链接有效并很好地解释了如何重命名来自reducer的状态。如果您不想阅读,请重命名减速器import或在combineReducer内部重命名它。

Example1:
import billReducer as billState from "./reducers";

Example2:
const rootReducer = combineReducer({billState: billReducer});

Using combineReducers