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

3

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

我认为当某些东西触发了一个动作时,redux通过reducer函数的堆栈返回表示应用程序状态的对象。 不幸的是,它返回一个带有{ 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}

我可能会错,但我相信这正是combineReducers()函数所做的事情。按预期工作。 - Gimby
这让我感觉很奇怪,因为在我看过的所有教程中,他们只有一个“common”对象,在存储状态中没有减速器名称 :( 无论如何,谢谢你的回答 :) - Dario
呸,教程。完美的自我破坏工具。不如试试Redux文档:http://redux.js.org/docs/api/combineReducers.html。 - Gimby
好的,现在我感觉自己很愚蠢... 非常感谢您的时间! :) - Dario
请查看 结构化Reducers - 使用combineReducers 文档页面,该页面讨论了像这样的常见问题。 - markerikson
2个回答

3

正如评论中已经回答的那样,combineReducers确实是这样工作的。如果您想链接多个reducer,以便操作将按顺序通过所有reducer并在每个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'] }); 

0
如果有人在找的话,上面评论中提供的链接已经失效了。这个链接可以正常使用,并且很好地解释了如何重命名来自reducers的state。如果您不想阅读,请将您的reducer重命名为import或在combineReducer内部进行重命名。
示例1:
import billReducer as billState from "./reducers"; 示例2:
const rootReducer = combineReducer({billState: billReducer}); 使用combineReducers

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接