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的更多相关文章

  1. A Bite Of React(2) Component, Props and State

    component component:用户自己定义的元素 const element = <Welcome name="Sara" />; class Welcome ...

  2. AntDesign-React与VUE有点不一样,第一篇深入了解React的概念之一:JSX

    AntDesign-React与VUE有点不一样,第一篇深入了解React的概念之一:JSX 一.什么是JSX 使用JSX声明一个变量(REACT当中的元素): const element =< ...

  3. React——嵌入已有项目 && jsx

    Add React to a Website React has been designed from the start for gradual adoption, and you can use ...

  4. 没有用到React,为什么我需要import引入React?

    没有用到React,为什么我需要import引入React? 本质上来说JSX是React.createElement(component, props, ...children)方法的语法糖. 所以 ...

  5. React系列之--props属性

    版权声明:本文为博主原创文章,未经博主允许不得转载. PS:转载请注明出处作者:TigerChain地址:http://www.jianshu.com/p/fa81cebac3ef本文出自TigerC ...

  6. 普通页面引入React(使用和不使用JSX)

    1. 不使用JSX 优点: 不用配置有关JSX的编译. 依赖语法: React.createElement(component/type, props, ...chilidren); //第一个参数可 ...

  7. React Native 开发之 (06) JSX

    一 React 1 React定义 React的GitHub地址是 https://github.com/facebook/react.它的官方介绍是 A JavaScript Library for ...

  8. React使用笔记1-React的JSX和Style

    React使用笔记1-React的JSX和Style Date: 2015-11-27 20:56 Category: Web Tags: JavaScript Author: 刘理想 [toc] 1 ...

  9. React.js学习之理解JSX和组件

    在开启JSX的学习旅程前,我们先了解一下React的基本原理.React本质上是一个"状态机",它只关心两件事:更新DOM和响应事件,React不处理Ajax.路由和数据存储,也不 ...

随机推荐

  1. Ubuntu 下更新或下载输入法(搜狗)

    ubuntu12.04的fcitx版本不支持,不满足依赖,需要更新fcitx 添加fcitx源添加fcitx源命令 : sudo add-apt-repository ppa:fcitx-team/n ...

  2. 数据库和java的类型转化

    mysql 的转化 count(1)   返回类型是bigint  对应的java 是 BigInteger cast(sum(name) as char)    sum转化为字符串

  3. Adobe Dreamweaver CC 2014 代码颜色目录 dw

    他的颜色代码配置文件,不在安装目录下,这让我好找啊~ C:\Users\Administrator\AppData\Roaming\Adobe\Dreamweaver CC 2014\zh_CN\Co ...

  4. pavenet资源

    网络结构:http://www.cnblogs.com/fariver/p/7449563.html 源码:https://github.com/sanghoon/pva-faster-rcnn 训练 ...

  5. tomcat修改编码格式

    在TOMCAT中的conf文件夹下server.xml中的 <Connector中添加两个设置useBodyEncodingForURI="true" //设置POST和GE ...

  6. selenium click radio

    radio = dr.find_element_by_xpath('//*[@id="contentTable"]/tbody/tr[1]/td[1]/input') webdri ...

  7. JavaScript exec()方法

    exec() 方法用于检索字符串中的正则表达式的匹配.返回一个数组,其中存放匹配的结果.如果未找到匹配,则返回值为 null. var str = "我今年25岁明年26岁后年27岁千年24 ...

  8. <Redis> 入门X 分布式锁

    分布式其实就是多进程的程序,当多个进程访问一个资源,会造成问题: 1.资源共享的竞争问题 2.数据的安全性 分布式锁的解决方案: 1.怎么去获取锁 数据库 zookeeper redis 2.怎么释放 ...

  9. 零基础入门学习Python(13)--元组:戴上了枷锁的列表

    前言 这节课我们讨论主题是元祖:我们有个小标题戴上了枷锁的列表 我们都知道早在300多年前,孟德斯鸠在变法的时候说过,一切拥有权力的人都容易被滥用权力,这是万古不变的一条经验.但是呢,凡是拥有大权利的 ...

  10. Python之文件处理-批量修改md文档内容

    目录 Python之文件处理-批量修改md文档内容 Python之文件处理-批量修改md文档内容 #!/usr/bin/env python # -*- coding:utf-8 -*- import ...