我有下面的动作和减速机:写入动作和减速高效,清洁(反应终极版)
操作:
import { OPEN_NODE, CLOSE_NODE, GET_NODES } from '../constants/NodeActionTypes';
export function openNode(path) {
return {
type: OPEN_NODE,
path: path
};
}
export function closeNode() {
return {
type: CLOSE_NODE
};
}
export function getNodes(path) {
return {
type: GET_NODES,
path: path
};
}
减速机:
export default function opener(state = initialState, action) {
switch (action.type) {
case OPEN_NODE:
var { path } = action
var {nodes} = getFileList(path)
return {
...state,
open:true,
nodes:nodes
};
case CLOSE_NODE:
return {
...state,
open:false
};
case GET_NODES:
var { path } = action
var {nodes} = getFileList(path)
return {
...state,
nodes:nodes
};
default:
return state;
}
}
显然,OPEN_NODE
包含GET_NODES
(只加上open:true
),但似乎有很多方法来组织代码:
包
GET_NODES
减速机到一个函数,调用这个在OPEN_NODE
,并添加open:true
。修改
openNode
行动,发送[OPEN_NODE, GET_NODES]
在一起,但如何写switch(action.type)
的情况?让
OPEN_NODE
减速派遣getNodes
动作来触发GET_NODES
减速
这是最好的?或者有其他更好的方法?
你基本上是问如何谱写自己的减速器。让我指出您正确的方向:https://egghead.io/courses/getting-started-with-redux。理解这门课程可以帮助你解决所有四个问题。 – xiaofan2406