惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
P
Proofpoint News Feed
The Hacker News
The Hacker News
Know Your Adversary
Know Your Adversary
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tenable Blog
AWS News Blog
AWS News Blog
S
Securelist
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
IT之家
IT之家
腾讯CDC
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
C
Check Point Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Engineering at Meta
Engineering at Meta
Latest news
Latest news
A
About on SuperTechFans
The Register - Security
The Register - Security
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
C
Cisco Blogs
T
Tailwind CSS Blog
Simon Willison's Weblog
Simon Willison's Weblog
阮一峰的网络日志
阮一峰的网络日志
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
T
Tor Project blog
L
Lohrmann on Cybersecurity
G
GRAHAM CLULEY
B
Blog RSS Feed
Scott Helme
Scott Helme
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
NISL@THU
NISL@THU
P
Privacy International News Feed
Security Latest
Security Latest
Recorded Future
Recorded Future
L
LangChain Blog
Cyberwarzone
Cyberwarzone
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
F
Fortinet All Blogs
O
OpenAI News
T
Threat Research - Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale

Li Hui Blog

工作流调研与实践经验 如何搭建私有仓库 Nexus 并配置 SSL 证书 从零开始搭建单节点 ELK 浅谈一个产品的用户引导 归档 GoCD 加入基础密码验证 搭建一套 gocd 的环境 如何使用 gitbook cli工具 React 组件示例 如何搭建第一个 React 程序 [SpringBoot 指南] 连接 mysql 数据库并进行增删改查测试 [SpringBoot 指南] 如何开始 Springboot 之旅 实现自己第一个接口 LeetCode刷题笔记 VPC笔记 读书的目的 [人文书籍] 把时间当作朋友 Docker基础命令 [技术书籍]JSON必知必会 Docker原理研究 使用Ubuntu 及宝塔搭建Ghost平台 [Linux]Ubuntu安装宝塔面板 2020年年终总结 CURL学习教程 2020年度计划记录 [安全系列]生成ssh证书 [CLI应用学习]时间使用GitHub CLI [linux系列] 修改主机用户名 轻松学CLI应用总章节 [Shell编程系列]基础教程2 [Shell编程系列]基础教程1 [安全相关]使用腾讯云子账号提高账号安全性 [Git教程系列]基础教程5 解决修改错分支的问题 [博客优化]为Wordpress加入伪静态 2020年的12本书籍 [Git系列教程]基本使用4 Git log的使用 [Git系列教程]Git基础操作3 [Git教程系列]基础操作2 [Git教程系列]Git初次配置 [周年总结]脉脉深情,深思志远,周年总结计划 [博客优化]为网站添加SSL证书 [博客优化]Wordpress加入redis服务器提高速度 开篇词 Posts Archive Li Hui Blog
React JSX语法再实践
2022-01-07 · via Li Hui Blog

我们上一节封装了一个按钮,这一节我们继续理解封装的概念和了解封装组件需要注意的事项

首先我们回顾上一篇最后的代码,代码如下

 1const Button = ({color,text}) =>{
 2    return {
 3        type: 'button',
 4        props: {
 5            className: `btn btn - $ {
 6                color
 7            }`,
 8            children: {
 9                type: 'em',
10                props: {
11                    children: text,
12                },
13            },
14        },
15    };
16}

我们最终调用的形式如下所示 Button({color:‘blue’, text:‘Confirm’}) 进行创建,其实我们发现 Buttonbutton 或者和 em 一样都可以 作为一个元素存在,我们可以以 Button 为基础创建特定属性的按钮,将其称为自定义类型的元素 或者可以称为组件元素。和上一节相同,我们可以采用 JSON 结构来描述它

1{
2 type: Button,
3 props: {
4  color: 'blue',
5  children: 'Confirm'
6 }
7}

上述结构和我们文中的第一个结果,因此可以进一步的对其中的元素做封装

比如我们对通常使用警告的颜色为红色, 但是我们红色提示的文字可能不同,此处我们封装一个危险的按钮的示例

1const dangerButton =({text}) => ({
2  type: Button,
3  props: {
4    color: 'red',
5    children: text  
6}
7})

比如书中给了一个很好的示例

 1const DeleteAccount = () = >({
 2    type: 'div',
 3    props: {
 4        children: [{
 5            type: 'p',
 6            props: {
 7                children: 'Are you sure?',
 8            },
 9        },
10        {
11            type: DangerButton,
12            props: {
13                children: 'Confirm',
14            },
15        },
16        {
17            type: Button,
18            props: {
19                color: 'blue',
20                children: 'Cancel',
21            },
22        }],
23    }
24});

这是一个弹出的删除账户的组件,其中有文字以及确定和取消的按钮,删除账户的组件就完成了。

我们后面将继续介绍 JSX 的语法