博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[React] Spread Component Props in JSX with React
阅读量:6476 次
发布时间:2019-06-23

本文共 1022 字,大约阅读时间需要 3 分钟。

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 }) => (  

{greeting}, {name}

);const greeting = "hello";const name = "John";const App = () =>
;render(
, document.getElementById("root"));

 

We can simply the code:

const App = () => 
;

or

const App = () => Demo({greeting, name})

 

But if we using Class Component instead of functional component like:

class Demo extends React.Component {  render() {    return (      

{
this.props.greeting}, {
this.props.name}

) }}

Then we have to use JSX approach or:

const App = () => React.createElement(Demo, {greeting, name});

 

转载地址:http://wrlko.baihongyu.com/

你可能感兴趣的文章
熟悉常用的Linux操作
查看>>
面象过程与面象对象
查看>>
谷歌设置支持webgl
查看>>
js的AJAX请求有关知识总结
查看>>
Eclipse添加新server时无法选择Tomcat7的问题
查看>>
nginx 配置https 负载均衡
查看>>
双拓扑排序 HDOJ 5098 Smart Software Installer
查看>>
三分 POJ 2420 A Star not a Tree?
查看>>
Leetcode 4 - median-of-two-sorted-arrays
查看>>
修改OBS为仅直播音频
查看>>
OCA读书笔记(3) - 使用DBCA创建Oracle数据库
查看>>
Python基础进阶之路(一)之运算符和输入输出
查看>>
阻塞非阻塞异步同步 io的关系
查看>>
ClickStat业务
查看>>
DMA32映射问题
查看>>
POJ 1269 Intersecting Lines(判断两直线位置关系)
查看>>
spring3.0.7中各个jar包的作用总结
查看>>
Windows 10 /win10 上使用GIT慢的问题,或者命令行反应慢的问题
查看>>
梯度下降(Gradient descent)
查看>>
Windows平台分布式架构实践 - 负载均衡
查看>>