当前位置:网站首页>策略模式代替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]);
}
边栏推荐
- Nearly 90% of servers can be saved, but the anti fraud efficiency has increased significantly. Why is PayPal's plan to break the "Ai memory wall" so cost-effective?
- Insert the laptop into the headset and still play it out (the personal test is valid)
- OSPF anti ring
- Software testing - Advanced
- 2022/7/16 周赛
- 基于stm32f103的智能风扇系统
- MAUI 框架入门学习05 MVVM数据模型理解
- Intensive reading series of papers
- DNS原理及解析过程
- 结构体通过成员变量获取主结构体地址(struct)
猜你喜欢
![[wechat applet] super easy to understand conditional rendering and list rendering](/img/02/c45ed1f4682a3436a6ef67003ca9d4.jpg)
[wechat applet] super easy to understand conditional rendering and list rendering

Heartless sword Chinese English bilingual poem 005 Lyric

51单片机一究到底输入模式

Nearly 90% of servers can be saved, but the anti fraud efficiency has increased significantly. Why is PayPal's plan to break the "Ai memory wall" so cost-effective?

Touchid and faceid~2

Intel experts share: how to program efficiently on XPU architecture? Zhiqiang Research Institute

wpf 找不到资源文件问题

若依框架包名修改器

对称加密与非对称加密

mysql中的视图、触发器和存储过程
随机推荐
安全第三天iptables防止nmap扫描以及binlog
Introduction au cadre Maui 05 compréhension du modèle de données mvvm
Swift 【Class】【struct】
使用kaggle跑李宏毅机器学习作业
Intel + Lenovo jointly launched open source cloud solutions
OSPF anti ring
VS Code 常用快捷键
牛客2021训练联盟热身训练赛Interstellar Love(并查集)
Intel helps open medical service and promote the intellectualization of ultrasonic prenatal examination
leetcode977. 有序数组的平方
Smart fan system based on STM32F103
IPhone device numbers of major models
Eas (energy aware scheduling) green energy-saving scheduler
Build a portrait matting server based on openvino model server
Unity - how to modify a package or localize it
By voting for the destruction of STI by Dao, seektiger is truly community driven
[database] must know and know at the end of the period ----- Chapter 12 database recovery
OSPF basic optimization
C # explain out output parameters in detail
结构体通过成员变量获取主结构体地址(struct)