这是编写Redux reducer的正确方法吗?

这是编写Redux reducer的正确方法吗?,redux,Redux,这是我的减速机代码,请让我知道这是正确的格式减速机应该是 我是Redux的新手,请帮我解决这个问题。我已经阅读了文档,但我没有了解其中的上下文,如果我错了,请让我知道我错在哪里 下面是我的代码 export default createReducer(initialState, { [SELECT_ENTITY]: (state, data) => { let selectedEntity; if (data.isSection) { // Assert the section e

这是我的减速机代码,请让我知道这是正确的格式减速机应该是

我是Redux的新手,请帮我解决这个问题。我已经阅读了文档,但我没有了解其中的上下文,如果我错了,请让我知道我错在哪里

下面是我的代码

export default createReducer(initialState, {

[SELECT_ENTITY]: (state, data) => {

let selectedEntity;

if (data.isSection) {

// Assert the section exists
selectedEntity = state.sections.filter(section => section.id ===     data.entityId)[0];

} else {

const lectures = state.sections.reduce((acc, section) => {

return (acc.concat(section.lectures));

},[]);

selectedEntity = lectures.filter(lecture => lecture.id === data.entityId)[0];

}

let newState;

let propertyWindowIsActive;

if (state.propertyWindowIsActive) {

if(state.entity.id === data.entityId  &&  state.isSection ===  data.isSection) {

// propertyWindowIsActive = false;

newState = Object.assign({}, state, {propertyWindowIsActive: false, entity:{}, isSection: null});

} else {

// propertyWindowIsActive = true;

newState = Object.assign({}, state, {entity: selectedEntity, isSection: data.isSection});

}

} else {

// propertyWindowIsActive = true;

newState = Object.assign({}, state, {propertyWindowIsActive: true, entity: selectedEntity, isSection: data.isSection});

}

return (newState);

}

}

发布问题时,请仔细设置代码格式。即使在将代码复制并粘贴到文本编辑器中并修复对齐方式之后,它看起来也不是有效的JavaScript

由于您是Redux新手,我建议您避免使用像“createReducer”这样的包装器。只要把你的减缩器写成纯函数就行了。它们应始终具有两个参数
状态
操作
,并返回新状态。减速器应该有一个基于
操作的开关语句。键入

请阅读有关减速器的Redux文档。

谢谢David的回复,所以你是说我不能使用减速机内部的逻辑,只有swich case检查动作并返回状态。你肯定会在减速机中使用逻辑。