定義
Components元件像函數可以回傳html元素。
名詞解釋
- 類別元件,會有些補充
- 路徑 react-tutorial\public\index.html
- react-tutorial\src\index.js This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>React App</title> </head> <body> <div id="root"></div> </body> </html> This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersimport React from 'react'; import ReactDOM from 'react-dom'; /* function Car() { return <h2>Hi, I am also a Car!</h2>; } */ class Car extends React.Component { render() { return <h2>Hi, I am a Car!</h2>; } } ReactDOM.render(<Car />, document.getElementById('root'));
- 函數元件
- 屬性像函數的參數,語法同HTML屬性,如This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class Car extends React.Component { render() { return <h2>I am a {this.props.color} Car!</h2>; } } ReactDOM.render(<Car color="red"/>, document.getElementById('root')); - 元件有一内建的state物件,可儲存元件的屬性值 This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class Car extends React.Component { constructor() { super(); this.state = {color: "red"}; } render() { return <h2>I am a {this.state.color} Car!</h2>; } } ReactDOM.render(<Car />, document.getElementById('root')); - 元件裏面的元件 This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class Car extends React.Component { render() { return <h2>I am a Car!</h2>; } } class Garage extends React.Component { render() { return ( <div> <h1>Who lives in my Garage?</h1> <Car /> </div> ); } } ReactDOM.render(<Garage />, document.getElementById('root')); - 元件在檔案中 This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
import React from 'react'; import ReactDOM from 'react-dom'; class Car extends React.Component { render() { return <h2>Hi, I am a Car!</h2>; } } export default Car; This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersimport React from 'react'; import ReactDOM from 'react-dom'; import Car from './App.js'; ReactDOM.render(<Car />, document.getElementById('root'));
如果你覺得這篇文章很有用,可以請我喝杯咖啡,讓我提供更多優質文章給您。感謝所有支持的朋友。
Vere Perrot 資訊人.科技人.行銷人,現為軟體分析師。定位自己為網路觀察家,永遠保持好奇心與熱情,學習跨領域新事物,希望最終能成為一個全方位的人。 Mail: vereperrot@gmail.com
沒有留言:
張貼留言