gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:39:44 25 4
gpt4 key购买 nike

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

我假设当某事触发一个 Action 时,通过 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
});

这是触发事件的 Action :

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 确实是这样工作的。如果你想链接 reducer 以便 action 将通过所有它们依次更新每个 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'] });

关于javascript - Redux - 应用程序状态以 reducer 的名称作为键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43185937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com