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

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Cisco Talos Blog
Cisco Talos Blog
T
Threat Research - Cisco Blogs
P
Privacy International News Feed
S
Schneier on Security
P
Privacy & Cybersecurity Law Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
Scott Helme
Scott Helme
人人都是产品经理
人人都是产品经理
G
GRAHAM CLULEY
O
OpenAI News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
PCI Perspectives
PCI Perspectives
GbyAI
GbyAI
宝玉的分享
宝玉的分享
Y
Y Combinator Blog
T
Troy Hunt's Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
腾讯CDC
C
Check Point Blog
Spread Privacy
Spread Privacy
L
LINUX DO - 最新话题
Recent Announcements
Recent Announcements
大猫的无限游戏
大猫的无限游戏
P
Palo Alto Networks Blog
Hacker News: Ask HN
Hacker News: Ask HN
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Hacker News
The Hacker News
H
Hacker News: Front Page
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
T
Tor Project blog
Martin Fowler
Martin Fowler
博客园 - 叶小钗
罗磊的独立博客
C
Cyber Attacks, Cyber Crime and Cyber Security
H
Heimdal Security Blog
V
Vulnerabilities – Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
Latest news
Latest news
WordPress大学
WordPress大学
G
Google Developers Blog
N
Netflix TechBlog - Medium
S
Security Affairs
S
Secure Thoughts
Know Your Adversary
Know Your Adversary

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 的语法