[Day29] React - 浅谈 Component 的Props

  • Props 是传递给 React Component 的参数。
  • Propse 透过HTML属性传递给Component。

使用Props

React Props 就像 JavaScript 中的函数参数和HTML 中的属性。

  • 要将 props 发送到Component中,方式与 HTML 属性差不多的语法:
const myElement = <Car brand="Toyota" />;
  • Component接收参数来做为props对象:
class Car extends React.Component {
    render(){
        return <h2>I love {this.props.brand}!</h2>
    }
};

完整程序码

import React from "react";
import ReactDOM from "react-dom";

class Car extends React.Component {
  render(){
      return <h2>I love {this.props.brand}!</h2>
  }
};

const myElement = <Car brand="Toyota" />;

ReactDOM.render(myElement, document.getElementById('root'));

输出结果>>>
I love Toyota!

 

参考来源:
https://www.w3schools.com/react/react_props.asp


<<:  设定字体颜色及文字大小、行距及间距

>>:  大共享时代系列_013_网页浏览器(web browser)

【Debug】PHP Key-Value 输入格式错误

Key-value 版本兼容警告 如果key不加引号 $data=[ john1=>1.2, ...

[Day25]solidity合约内容讲解3

先公布昨天小练习的答案: 小练习:宣告一个公开、可读不可写、名为abc的function,参数a为...

【C#】Behavioral Patterns Mediator Mode

The Mediator design pattern defines an object that...

Day 13 ( 中级 ) 大型数字 ( 图形数字 )

大型数字 ( 图形数字 ) 教学原文参考:大型数字 ( 图形数字 ) 如果要在 Scratch 3 ...

26 可以按按钮出牌才算出牌

出牌 出牌跟之前的游戏准备按钮一样 帮一个元件加上 phx-clixk 再绑上对应的动作, 唯一的差...