Redux reducers 初始化相同的状态键
Redux reducers initializing same state key
我在摆弄 'simplest-redux-example' on github,我添加了第二个递减 state.count 的减速器。如果我在 switch case 语句中有增量和减量缩减器,它工作正常。我想要执行的练习是将 reducer 拆分为尽可能多的模块化部分。此代码抛出一个错误,指出计数未定义。
import React from 'react';
import { createStore, combineReducers } from 'redux';
import { Provider, connect } from 'react-redux';
// React component
class Counter extends React.Component {
render(){
const { value, onIncreaseClick, onDecreaseClick } = this.props;
return (
<div>
<span>{value}</span>
<button onClick={onIncreaseClick}>Increase</button>
<button onClick={onDecreaseClick}>Decrease</button>
</div>
);
}
}
// Action:
const increaseAction = {type: 'increase'};
const decreaseAction = {type: 'decrease'};
// Reducer:
function counter(state, action) {
let count = state.count;
switch(action.type){
case 'increase':
return {count: count+1};
default:
return state;
}
}
function decrementer(state, action) {
let count = state.count;
switch(action.type){
case 'decrease':
return {count: count -1};
default:
return state;
}
}
const rootReducer = combineReducers({
counter,
decrementer
})
// Store:
let store = createStore(rootReducer, {count: 0});
// Map Redux state to component props
function mapStateToProps(state) {
console.log("mapStatetoProps heyyyy");
return {
value: state.count
};
}
// Map Redux actions to component props
function mapDispatchToProps(dispatch) {
console.log("mapDispatchtoProps heyyyy");
return {
onIncreaseClick: () => dispatch(increaseAction),
onDecreaseClick: () => dispatch(decreaseAction)
};
}
// Connected Component:
let App = connect(
mapStateToProps,
mapDispatchToProps
)(Counter);
React.render(
<Provider store={store}>
{() => <App />}
</Provider>,
document.getElementById('root')
);
传递给 combineReducers
的 reducer 获得状态对象的不同 部分 。
The resulting reducer calls every child reducer, and gather their results into a single state object. The shape of the state object matches the keys of the passed reducers
.
(强调我的)
所以,内部状态对象看起来像
{
counter: result of passing `state.counter` into counter reducer
decrementer: result of passing `state.decrementer` into decrementer reducer
}
这类似于在 flux 应用程序中拥有单独的商店,其中每个商店都操作自己的 "piece" 全局应用程序状态。
由于您实际上希望这两个 reducer 在状态对象的 相同 部分上工作,因此您实际上想要更像 reduce-reducers 的东西,尽管这很容易自己实现——只需将状态依次传递给每个 reducer,用来自每个 reducer 的新状态减少初始状态。
其实就这么简单,实现就几行:
export default function reduceReducers(...reducers) {
return (previous, current) =>
reducers.reduce(
(p, r) => r(p, current),
previous
);
}
你的rootReducer
会是
const rootReducer = reduceReducers(counter, decrementer);
你太接近了!问题在于,当您使用 combineReducers 时,它实际上会拆分 "state",这样您输入的 reducer 的状态就是 "state" 对象的属性。
因此,为了给它们提供默认参数,如下所示:
let store = createStore(rootReducer, {counter: {count: 0}, decrementer: {count:0}});
我在摆弄 'simplest-redux-example' on github,我添加了第二个递减 state.count 的减速器。如果我在 switch case 语句中有增量和减量缩减器,它工作正常。我想要执行的练习是将 reducer 拆分为尽可能多的模块化部分。此代码抛出一个错误,指出计数未定义。
import React from 'react';
import { createStore, combineReducers } from 'redux';
import { Provider, connect } from 'react-redux';
// React component
class Counter extends React.Component {
render(){
const { value, onIncreaseClick, onDecreaseClick } = this.props;
return (
<div>
<span>{value}</span>
<button onClick={onIncreaseClick}>Increase</button>
<button onClick={onDecreaseClick}>Decrease</button>
</div>
);
}
}
// Action:
const increaseAction = {type: 'increase'};
const decreaseAction = {type: 'decrease'};
// Reducer:
function counter(state, action) {
let count = state.count;
switch(action.type){
case 'increase':
return {count: count+1};
default:
return state;
}
}
function decrementer(state, action) {
let count = state.count;
switch(action.type){
case 'decrease':
return {count: count -1};
default:
return state;
}
}
const rootReducer = combineReducers({
counter,
decrementer
})
// Store:
let store = createStore(rootReducer, {count: 0});
// Map Redux state to component props
function mapStateToProps(state) {
console.log("mapStatetoProps heyyyy");
return {
value: state.count
};
}
// Map Redux actions to component props
function mapDispatchToProps(dispatch) {
console.log("mapDispatchtoProps heyyyy");
return {
onIncreaseClick: () => dispatch(increaseAction),
onDecreaseClick: () => dispatch(decreaseAction)
};
}
// Connected Component:
let App = connect(
mapStateToProps,
mapDispatchToProps
)(Counter);
React.render(
<Provider store={store}>
{() => <App />}
</Provider>,
document.getElementById('root')
);
传递给 combineReducers
的 reducer 获得状态对象的不同 部分 。
The resulting reducer calls every child reducer, and gather their results into a single state object. The shape of the state object matches the keys of the passed
reducers
.
(强调我的)
所以,内部状态对象看起来像
{
counter: result of passing `state.counter` into counter reducer
decrementer: result of passing `state.decrementer` into decrementer reducer
}
这类似于在 flux 应用程序中拥有单独的商店,其中每个商店都操作自己的 "piece" 全局应用程序状态。
由于您实际上希望这两个 reducer 在状态对象的 相同 部分上工作,因此您实际上想要更像 reduce-reducers 的东西,尽管这很容易自己实现——只需将状态依次传递给每个 reducer,用来自每个 reducer 的新状态减少初始状态。
其实就这么简单,实现就几行:
export default function reduceReducers(...reducers) {
return (previous, current) =>
reducers.reduce(
(p, r) => r(p, current),
previous
);
}
你的rootReducer
会是
const rootReducer = reduceReducers(counter, decrementer);
你太接近了!问题在于,当您使用 combineReducers 时,它实际上会拆分 "state",这样您输入的 reducer 的状态就是 "state" 对象的属性。
因此,为了给它们提供默认参数,如下所示:
let store = createStore(rootReducer, {counter: {count: 0}, decrementer: {count:0}});