gpt4 book ai didi

javascript - 我可以在 Redux 中从一个 reducer 获得多个状态吗?

转载 作者:行者123 更新时间:2023-11-29 18:40:49 24 4
gpt4 key购买 nike

假设我有下一个 reducer :

export default (state = [], { type, payload }) => {
switch (type) {

case FETCH_POKEMONS:
const objeto = {};
payload.forEach((conexion) => {
objeto[conexion.name] = conexion
});
return objeto;

case FETCH_POKEMON:
return { ...state, ...payload }

default:
return state
}
}

我将有一个像这样的 combineReducers:

export default combineReducers({
pokemons: pokemonReducers,
});

但我想为 FETCH_POKEMONS Action 设置 pokemons 状态,为 FETCH_POKEMON 设置另一个名为 pokemon 的状态采取行动。如何从一个 reducer 文件中导出 combineReducers 中的两个状态?

最佳答案

这是反模式,最接近的做法是从您的文件中导出 2 个 reducer,每个用例一个

export const reducer1 = (state = [], { type, payload }) => {
switch (type) {

case FETCH_POKEMONS:
const objeto = {};
payload.forEach((conexion) => {
objeto[conexion.name] = conexion
});
return objeto;

default:
return state
}
}

export const reducer2 = (state = [], { type, payload }) => {
switch (type) {

case FETCH_POKEMON:
return { ...state, ...payload }

default:
return state
}
}

关于javascript - 我可以在 Redux 中从一个 reducer 获得多个状态吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57116512/

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