通常我们会做以下事情来定义一个 set redux action&reducer with redux-actions library
export const {
open,
close,
send
} = createActions({
OPEN: payload => ({
payload
}),
CLOSE: payload => ({
payload
}),
SEND: payload => ({
payload,
})
});
export const combinedReducer = createActions({
[open]: (state, action) => { /*you do someting*/ },
[close]: (state, action) => { /*you do someting*/ },
[send]: (state, action) => { /*you do someting*/ }
});
/*in some where , we are going to handle the a triggered action type respectively in a switch statement.
but we have to use string concatenation to make the switch express strict equal pass , any graceful solution ? */
switch (action.type) {
case open + "":
//do someting
break;
case close + "":
//do someting
break;
case send + "":
//do someting
break;
}
上面生成的变量open、close、send实际上是 函数类型,它们的 toString()被 redux-action lib 覆盖以导出像"OPEN"、"CLOSE"、"send"这样的字符串
但是,如果我们想在 switch 语句中重用这些动作类型,我们必须以这种尴尬的方式连接 ''来传递 switch 表达式。
在处理执行严格相等比较的 switch 语句解析时,是否有任何优雅的方法可以避免这种愚蠢的代码===
提前致谢。