reactjs - TS 缩短代码并在 if 表达式中重复
问题描述
我想缩短我目前看起来像这样的代码:
onClick={value => {
if (value === "valA") {
Mutation.mutate({filter, source: "sourceA" }, {someRepetitiveCode}},
} else if (value === "valB") {
Mutation.mutate({filter, source: "sourceB" }, {someRepetitiveCode}},
else {
Mutation.mutate({filter, source: undefined },{someRepetitiveCode}}
}
代码中有很多重复,所以我想问是否有办法缩短该表达式,例如使用 lambda 表达式。
解决方案
不完全确定你想要什么,但在我看来你想source
根据value
. 我认为这可能是您正在寻找的:
source = when(value) {
"valA" -> "sourceA"
"valB" -> "sourceB"
else -> "undefined"
}
推荐阅读
- ios - IOS 11.4.1 上的 AVFoundation、AVAudioSessionRouteChange 行为不正确
- php - Laravel:如何在子进程中通过 Eloquent 更新 MySQL?
- windows - 通过 Electron 访问当前高亮文本
- php - 1 个模型中的 2 个查询 (CodeIgniter)
- javascript - 自将 ASP.NET 站点从 HTTP 更改为 HTTPS 后,自动完成/即时搜索结果框停止工作
- python - 将函数的输出保存在python的csv文件中
- vaadin - 在 UI 中单独加载组件
- oracle - Oracle XMLQuery - 显示带有两个 XML 节点的逗号分隔列表
- c# - ASP.NET.MVC 5 为控制器的操作添加权限
- vba - VBA 性能,应该使用什么命令来禁用/启用屏幕更新