当前位置:网站首页>策略模式代替if-else
策略模式代替if-else
2022-07-17 04:10:00 【鳕鱼&羚羊】
采用key-value的形式替换繁琐的if-else (高端大气上档次)
if-else或switch写法:
// if-else
const onTabChange = (key: string) => {
if(key=="home") setTabContent(<Home />)
else if(key=="search") setTabContent(<Search />)
else if(key=="article") setTabContent(<Article />)
else if(key=="video") setTabContent(<Video />)
}
// switch
const onTabChange = (key: string) => {
switch (key) {
case "home":
setTabContent(<Home />);
break;
case "search":
setTabContent(<Search />);
break;
case "article":
setTabContent(<Article />);
break;
case "video":
setTabContent(<Video />);
break;
default:
return false;
break;
}
策略模式改造:
const onTabChange = (key: string) => {
const tab = {
"home": <Home />,
"search": <Search />,
"article": <Article />,
"video": <Video />,
}
setTabContent(tab[key]);
}
边栏推荐
- The adaptation of go language under windows10:vscode
- WPF cannot find resource file problem
- Xcode11 add a boot page (the launch images source option is missing after the upgrade)
- windows10:vscode下go语言的适配
- [ruoyi Vue plus] learning notes 30 - redisson (VI) bounded blocking queue (redisson source code + Lua script)
- Unity - how to modify a package or localize it
- 对称加密与非对称加密
- [super cloud terminal to create a leading opportunity] local computing cloud management, Intel helps digitalize Education
- 分布式笔记(02)— 分布式缓存之 Redis(用途、特点、高可用方案 Redis Cluster、Twemproxy、Codis 简要说明)
- Intel + Lenovo jointly launched open source cloud solutions
猜你喜欢

【微信小程序】超易懂的条件渲染和列表渲染

SQL interface switching cannot obtain focus

Introduction to Maui framework 05 MVVM data model understanding

OSPF anti ring

Thesis research NLP

CAD video course recommendation station B

06 MAUI,WPF使用 MVVM Toolkit 框架 构建 MVVM 程序

iptables防止nmap扫描以及binlog详解
![[ruoyi Vue plus] learning notes 30 - redisson (VI) bounded blocking queue (redisson source code + Lua script)](/img/56/f52d13f86764768eee190e22ba136a.png)
[ruoyi Vue plus] learning notes 30 - redisson (VI) bounded blocking queue (redisson source code + Lua script)

OSPF基础优化
随机推荐
Avplayer adds playback progress monitoring
基于STM32的SG90舵机实验含代码(HAL库)
Software testing - Advanced
基于stm32f103的智能风扇系统
Live broadcast of cloud intelligence face to face is waiting for you: computing power redefines productivity
小程序毕设作品之微信电子书阅读小程序毕业设计(1)开发概要
V4L2学习资料收集
Wechat e-book reading of small program graduation design (5) task book
PAC Decade: witness HPC from CPU era to XPU Era
分布式笔记(02)— 分布式缓存之 Redis(用途、特点、高可用方案 Redis Cluster、Twemproxy、Codis 简要说明)
Intel + Lenovo jointly launched open source cloud solutions
STM32——定时器系列(二)通用定时器
使用kaggle跑李宏毅机器学习作业
MAUI 框架入门学习05 MVVM数据模型理解
Wechat e-book reading of small program graduation project (4) opening report
C# 详解out输出参数
Impersonate the server for requests
[database] must know at the end of the term ----- Chapter VII database integrity
word使用技巧
OSPF anti ring