gpt4 book ai didi

javascript - 使用 `` `for...in ``` 实现 combineReducers?

转载 作者:行者123 更新时间:2023-11-29 19:15:21 25 4
gpt4 key购买 nike

egghead.io series on Redux 的第 16 课中,在看 Dan 是如何做到的之前,我尝试实现了自己的 combineReducers 函数。我得到了以下。我尝试在像这样传入的子 reducer (todosvisibilityFilter)上使用 for ... in

const combineReducers  = (reducers) => {
return (state,action) => {
let temp = {};
for (let i in reducers) {
temp[i] = reducers[i](state,action)
}
return temp;

}
}

这是行不通的。当我使用 expect 库对其进行测试时,我在控制台中收到以下错误。奇怪的是,如果我没记错的话,调用 todos reducer 的状态似乎已嵌套到 visibilityFilter reducer 的调用中。这很奇怪,因为我的代码显示它们在返回的对象中是截然不同的字段。

Uncaught Error: Expected { todos: [ { completed: false, id: 1, text: 'Go shopping' } ], visibilityFilter: { todos: [ { completed: false, id: 0, text: 'Learn Redux' } ], visibilityFilter: 'SHOW_ALL' } } to equal { todos: [ { completed: false, id: 0, text: 'Learn Redux' }, { completed: false, id: 1, text: 'Go shopping' } ], visibilityFilter: 'SHOW_ALL' }

我的测试代码是

const testTodoApp = () => {
const stateBefore = {
todos: [{id: 0, text:'Learn Redux', completed: false}],
visibilityFilter: 'SHOW_ALL',
};

// action is an object. with a defined type property.
const action = {
type: 'ADD_TODO',
id: 1,
text: 'Go shopping',
};

const stateAfter = {
todos: [{id: 0, text:'Learn Redux', completed: false},
{id: 1, text:'Go shopping', completed: false},
],
visibilityFilter: 'SHOW_ALL',
};

deepFreeze(stateBefore);
deepFreeze(action);

expect(
todoApp(stateBefore, action)
).toEqual(stateAfter);
console.log("Test passed: todoApp")

}

testTodoApp();

如果我使用内置的 combineReducers,这个测试将会通过。子 reducer 和对 combineReducers 的调用如下:

const todo = (state = {} ,action) => {
switch (action.type) {

case 'ADD_TODO':
return {
id: action.id, text: action.text, completed: false,
};

case 'TOGGLE_TODO':
if (state.id !== action.id) {
return state;
}
return {
...state, completed: !state.completed,
};

default:
return state;

}

}
const todos = (state=[], action) =>{

switch (action.type) {
case 'ADD_TODO':
console.log('ADD_TODO switch selected')
return [
...state,
todo(undefined,action),
];

case 'TOGGLE_TODO':
console.log('TOGGLE_TODO switch selected')

return state.map( t => todo(t, action))

default:
console.log('default switch selected')
return state;
}
}

const visibilityFilter = (
state = 'SHOW_ALL',
action
) => {
switch (action.type) {
case 'SET_VISIBILITY_FILTER':
return action.filter;
default:
return state;
}
}


const todoApp = combineReducers({
todos,
visibilityFilter,
})

我的问题是:

  1. 在我的代码中是什么导致了一个 reducer 嵌套在另一个 reducer 中?
  2. 我知道 Dan​​ 使用的是 reduce,但出于教学原因,我该如何使用 for ... in 模式来实现 combineReducers?
  3. 之后,您能否评论一下对此类应用程序使用 for ... in 的适当性,如果它是一个糟糕的模式,是什么让它如此?

最佳答案

我刚刚意识到 todos reducer 和 visibilityFilter reducer 必须传递与它们的键对应的组合状态部分,不是整个组合状态。所以工作代码应该是这样的,我在第 5 行向状态的相应部分添加了一个对象访问器。

const combineReducers  = (reducers) => {
return (state,action) => {
let temp = {};
for (let i in reducers) {
temp[i] = reducers[i](state[i],action)
}
return temp;

}
}

关于javascript - 使用 `` `for...in ``` 实现 combineReducers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35813168/

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