当前位置:网站首页>策略模式代替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]);
}
边栏推荐
- C# 详解out输出参数
- donet framework4. X==windows form application new project, through system Data. SqlClient connects to sqlserver to query
- 小程序毕设作品之微信电子书阅读小程序毕业设计(3)后台功能
- 牛客2021训练联盟热身训练赛Interstellar Love(并查集)
- Xdc 2022 Intel technology special session: Intel Software and hardware technology builds the cornerstone of cloud computing architecture
- 笔记本电脑插入耳机仍然外放(亲测有效)
- Deconstruction of typescript array / object / string / function parameters
- [database] must know and know at the end of the period ----- Chapter 12 database recovery
- iptables防止nmap扫描以及binlog详解
- 结构体通过成员变量获取主结构体地址(struct)
猜你喜欢

微信附近的人小程序怎么开(开通附近小程序的方法)

MySQL中的删除:delete、drop、Truncate三者的区别

分布式笔记(02)— 分布式缓存之 Redis(用途、特点、高可用方案 Redis Cluster、Twemproxy、Codis 简要说明)

若依框架包名修改器

CAD视频课程推荐 b站

OSPF routing control, anti ring related knowledge

Openresty as a static resource server

基于stm32f103的智能风扇系统
![[seventh issue of notebook series] download and use of openvino pre training model](/img/74/d5958137ddabadef1724afda5656e1.png)
[seventh issue of notebook series] download and use of openvino pre training model

【微信小程序】超易懂的条件渲染和列表渲染
随机推荐
DNS原理及解析过程
Codeforces Round #807 (Div. 2) A~D
HCR慧辰北坡而行,一只游入数字营销服务的巨兽
Sg90 actuator experiment based on STM32, including code (HAL Library)
CAD video course recommendation station B
MAUI 框架入门学习05 MVVM数据模型理解
[database] must know at the end of the term ----- Chapter VII database integrity
donet framework4. X==windows form application new project, through system Data. SqlClient connects to sqlserver to query
C# 使用this关键字串联构造函数调用方法
若依框架包名修改器
[database] must know and know at the end of the period ----- Chapter 12 database recovery
If by frame package name modifier
Wechat e-book reading of small program graduation design (5) task book
机器学习11:代价敏感学习
C # explain out output parameters in detail
CAD视频课程推荐 b站
Technical writing guide for programmers to leave work early
tf.AUTO_REUSE的作用
Xdc 2022 Intel technology special session: Intel Software and hardware technology builds the cornerstone of cloud computing architecture
64. Minimum path sum: given an M x n grid containing non negative integers, please find a path from the upper left corner to the lower right corner, so that the sum of the numbers on the path is the m