[React] Spread Component Props in JSX with React
You often find duplication between the name of a prop and a variable you will assign to the prop. JSX allows you to spread an object containing your named props into your Component which enables you to avoid the repetition of matching prop names and variable names. This lessons covers spreading JSX component props in both pure components and class components.
We have code:
import React from "react";
import { render } from "react-dom"; const Demo = ({ greeting, name }) => (
<h2>
{greeting}, {name}
</h2>
); const greeting = "hello";
const name = "John"; const App = () => <Demo greeting={greeting} name={name} />; render(<App />, document.getElementById("root"));
We can simply the code:
const App = () => <Demo {...{greeting, name}} />;
or
const App = () => Demo({greeting, name})
But if we using Class Component instead of functional component like:
class Demo extends React.Component {
render() {
return (
<h2>
{this.props.greeting}, {this.props.name}
</h2>
)
}
}
Then we have to use JSX approach or:
const App = () => React.createElement(Demo, {greeting, name});
[React] Spread Component Props in JSX with React的更多相关文章
- A Bite Of React(2) Component, Props and State
component component:用户自己定义的元素 const element = <Welcome name="Sara" />; class Welcome ...
- AntDesign-React与VUE有点不一样,第一篇深入了解React的概念之一:JSX
AntDesign-React与VUE有点不一样,第一篇深入了解React的概念之一:JSX 一.什么是JSX 使用JSX声明一个变量(REACT当中的元素): const element =< ...
- React——嵌入已有项目 && jsx
Add React to a Website React has been designed from the start for gradual adoption, and you can use ...
- 没有用到React,为什么我需要import引入React?
没有用到React,为什么我需要import引入React? 本质上来说JSX是React.createElement(component, props, ...children)方法的语法糖. 所以 ...
- React系列之--props属性
版权声明:本文为博主原创文章,未经博主允许不得转载. PS:转载请注明出处作者:TigerChain地址:http://www.jianshu.com/p/fa81cebac3ef本文出自TigerC ...
- 普通页面引入React(使用和不使用JSX)
1. 不使用JSX 优点: 不用配置有关JSX的编译. 依赖语法: React.createElement(component/type, props, ...chilidren); //第一个参数可 ...
- React Native 开发之 (06) JSX
一 React 1 React定义 React的GitHub地址是 https://github.com/facebook/react.它的官方介绍是 A JavaScript Library for ...
- React使用笔记1-React的JSX和Style
React使用笔记1-React的JSX和Style Date: 2015-11-27 20:56 Category: Web Tags: JavaScript Author: 刘理想 [toc] 1 ...
- React.js学习之理解JSX和组件
在开启JSX的学习旅程前,我们先了解一下React的基本原理.React本质上是一个"状态机",它只关心两件事:更新DOM和响应事件,React不处理Ajax.路由和数据存储,也不 ...
随机推荐
- MySql 基础知识-常用命令及sql语句
一.常用mysql命令行命令 1,启动mysql服务 net start mysql. 停止mysql服务 net stop mysql 2,netstart -na|findstr 330 ...
- 迅为电子4.3寸CAN总线工业平板电脑简介
型号:iTOP-HMI043-C 4.3寸CAN总线工业平板电脑支持CAN通讯显示器,显示:显示尺寸:4.3英寸:分辨率:480×272 TFT液晶 65536色 :接口:支持CAN 2.0B:USB ...
- h5混编问题总结
h5混编总结: 1.fragment 格式错误导致跳转混乱的问题:修改格式: 2.有缓存回退js不执行问题:未解决: 3.无缓存跨域回退白屏问题:解决跨域问题. 4.
- WOJ600——水题
第一次做本校OJ的题,被坑的好惨啊! 题目:600.Minimum Distance 题目大意:给定平面上3个点A.B.C,求平面上的任一顶点P,使得|PA|+2|PB|+3|PC|. 由于刚好在这 ...
- 关于动态添加的html元素绑定的事件不生效的解决办法
1.可以通过行内添加事件的方法,比如onclick="fn()"; 在js中写好方法名对应的方法就可以了,如果绑定方法的元素太多 2.jquery的on事件绑定 //on事件可以给 ...
- 06网络通信udp-tcp、正则
一. udp网络程序 1. udp网络程序-发送数据 1)创建客户端套接字 2)发送/接收数据 3)关闭套接字 from socket import * # 1. 创建udp套接字 udp_so ...
- 笔试算法题(54):快速排序实现之单向扫描、双向扫描(single-direction scanning, bidirectional scanning of Quick Sort)
议题:快速排序实现之一(单向遍历) 分析: 算法原理:主要由两部分组成,一部分是递归部分QuickSort,它将调用partition进行划分,并取得划分元素P,然后分别对P之前的部分和P 之后的部分 ...
- Python 绑定方法与非绑定方法
用到的: import uuid -------------- uuid是128位的全局唯一标识符, 通常用32位的一个字符串的形式来表现 uuid.uuid1() ------------- ...
- Go:面向"对象"
一.封装 封装的实现步骤: 将结构体.字段的首字母小写(不能被导出): 给结构体所在的包提供一个工厂模式的函数,首字母大写.类似一个构造函数: 提供一个首字母大写的方法,由于获取结构体属性的值. 二. ...
- table中JS选取行列
<table border="1" align="center" style="width: 500px" id="doct ...