Redux 在减速机中编写嵌套开关大小写是一种好做法吗
我是react redux应用程序的新手。我的行为由如下一个小层次组成:Redux 在减速机中编写嵌套开关大小写是一种好做法吗,redux,react-redux,switch-statement,Redux,React Redux,Switch Statement,我是react redux应用程序的新手。我的行为由如下一个小层次组成: { type:'FITLER', filterType: 'FILTER_COUNTRY', payload: countries } 在我的reducer中,除其他函数外,我将我的reducer之一写为: function FilterValue(state, action){ switch(action.type){ CASE FILTER:break;
{
type:'FITLER',
filterType: 'FILTER_COUNTRY',
payload: countries
}
在我的reducer中,除其他函数外,我将我的reducer之一写为:
function FilterValue(state, action){
switch(action.type){
CASE FILTER:break;
CASE FILTER_CLEAR_ALL:break;
default:
}
}
我想知道是否应该为我的典型案例制作嵌套的switch语句,如:
function FilterValue(state, action){
switch(action.type){
CASE FILTER:
switch(action.filterType){
CASE FILTER_COUNTRY:break;
CASE FILTER_REGION: break;
default:
}
CASE FILTER_CLEAR_ALL:
default:
}
}
我查阅了这些文章,并提出了一些问题,但没有一篇回答了有关这种编程实践的问题
编辑:我已经在使用Reducer和Thunk中间件了。我的问题只是关于嵌套开关箱。嵌套异径管:
嵌套异径管是一种不好的做法。您将希望尽可能使您的减缩器(和状态切片)保持平坦。因此,在更新状态片方面,拆分将产生更好的开发体验。检查
关于开关箱:
考虑从开关结构重构减速器
export function todos(state = [], action) {
switch (action.type) {
case ActionTypes.ADD_TODO:
const text = action.text.trim()
return [...state, text]
default:
return state
}
}
export const todos = createReducer([], {
[ActionTypes.ADD_TODO]: (state, action) => {
const text = action.text.trim()
return [...state, text]
}
})
到哈希表格式
export function todos(state = [], action) {
switch (action.type) {
case ActionTypes.ADD_TODO:
const text = action.text.trim()
return [...state, text]
default:
return state
}
}
export const todos = createReducer([], {
[ActionTypes.ADD_TODO]: (state, action) => {
const text = action.text.trim()
return [...state, text]
}
})
可以编写以下帮助程序来完成此操作:
function createReducer(initialState, handlers) {
return function reducer(state = initialState, action) {
if (handlers.hasOwnProperty(action.type)) {
return handlers[action.type](state, action)
} else {
return state
}
}
}
使用此方法的主要好处:
它并没有真正回答我的问题。