網(wǎng)站首頁 編程語言 正文
創(chuàng)建組件
函數(shù)組件
函數(shù)組件:使用JS的函數(shù)或者箭頭函數(shù)創(chuàng)建的組件
- 使用 JS 的函數(shù)(或箭頭函數(shù))創(chuàng)建的組件,叫做函數(shù)組件
- 約定1:函數(shù)名稱必須以大寫字母開頭,React 據(jù)此區(qū)分組件和普通的 HTML
- 約定2:函數(shù)組件必須有返回值,表示該組件的 UI 結(jié)構(gòu)
// 1. 導(dǎo)包 import React from 'react' import ReactDom from 'react-dom/client' // 2. 創(chuàng)建 函數(shù)式組件 // 普通函數(shù)或者箭頭函數(shù)創(chuàng)建組件,首字母大寫 // 組件必須要有返回值 function Music() { return ( <div> <h1>haha</h1> </div> ) } const VNode = ( <div> <Music></Music> </div> ) // 3. 掛載 ReactDom.createRoot(document.querySelector('#root')).render(VNode)
類組件
類組件:使用 ES6 的 class 創(chuàng)建的組件,叫做類(class)組件
- 約定1:類名稱也必須以大寫字母開頭
- 約定2:類組件應(yīng)該繼承?
React.Component
?父類,從而使用父類中提供的方法或?qū)傩?/li> - 約定3:類組件必須提供?
render
?方法 - 約定4:render 方法必須有返回值,表示該組件的 UI 結(jié)構(gòu)
// 1. 導(dǎo)包 import React from 'react' import ReactDom from 'react-dom/client' // 2. class Hello extends React.Component { render() { return <h1>哈哈</h1> } } const VNode = ( <> <Hello></Hello> </> ) // 3. 掛載 ReactDom.createRoot(document.querySelector('#root')).render(VNode)
組件提取到單獨(dú)的文件中
在components
文件夾下,創(chuàng)建函數(shù)組件hello.js
,類組件home.js
,
hello.js
const Hello = () => <h1>我是hello組件</h1> export default Hello
home.js
import React from 'react' class Home extends React.Component { render() { return <h2>home</h2> } } export default Home
在index.js
中導(dǎo)入
// 1. 導(dǎo)包 import React from 'react' import ReactDom from 'react-dom/client' // 導(dǎo)入組件 import Hello from './components/hello' import Home from './components/home' // 2. 創(chuàng)建虛擬DOM const App = ( <> <Hello></Hello> <Home></Home> </> ) // 3. 掛載 ReactDom.createRoot(document.querySelector('#root')).render(App)
有狀態(tài)組件與無狀態(tài)組件
狀態(tài)即數(shù)據(jù)
- 函數(shù)組件又叫做無狀態(tài)組件 函數(shù)組件是不能自己提供數(shù)據(jù) 【前提:基于hooks之前說的 16.8之前】
- 類組件又叫做有狀態(tài)組件類組件可以自己提供數(shù)據(jù),數(shù)據(jù)如果發(fā)生了改變,內(nèi)容會自動(dòng)的更新
- 組件的私有數(shù)據(jù)也稱為狀態(tài) ,當(dāng)組件的狀態(tài)發(fā)生了改變,頁面結(jié)構(gòu)也就發(fā)生了改變。【數(shù)據(jù)驅(qū)動(dòng)視圖】
- 函數(shù)組件是沒有狀態(tài)的,只負(fù)責(zé)頁面的展示(靜態(tài),不會發(fā)生變化)性能比較高
- 類組件有自己的狀態(tài),負(fù)責(zé)更新UI,只要類組件的數(shù)據(jù)發(fā)生了改變,UI就會發(fā)生更新(動(dòng)態(tài))。
- 在復(fù)雜的項(xiàng)目中,一般都是由函數(shù)組件和類組件共同配合來完成的。
// 1. 導(dǎo)包 import React from "react" import ReactDom from 'react-dom/client' // 函數(shù)組件 沒有狀態(tài) 僅僅做一些數(shù)據(jù)展示的工作,可以使用函數(shù)組件 // function App() { // return ( // <div>我是組件</div> // ) // } // 類組件 有狀態(tài) 如果有狀態(tài),狀態(tài)需要切換,更新視圖 用類組件 class App extends React.Component { render() { return ( <h1>我是類組件</h1> ) } } const VNode = ( <div> <App></App> </div> ) ReactDom.createRoot(document.querySelector('#root')).render(VNode)
類組件的狀態(tài)
- 狀態(tài)
state
即數(shù)據(jù),是組件內(nèi)部的私有數(shù)據(jù),只有在組件內(nèi)部可以使用 - state的值是一個(gè)對象,表示一個(gè)組件中可以有多個(gè)數(shù)據(jù)
- 通過
this.state.xxx
來獲取狀態(tài)
// 1. 導(dǎo)包 import React from "react" import ReactDom from 'react-dom/client' // 類組件 有狀態(tài) 如果有狀態(tài),狀態(tài)需要切換,更新視圖 用類組件 class App extends React.Component { // state節(jié)點(diǎn)中提供狀態(tài) // 通過 this.state.xxx 來獲取狀態(tài) state = { name: 'Tt', age: 17 } render() { return ( <h1>{this.state.name} ----- {this.state.age}</h1> ) } } const VNode = ( <div> <App></App> </div> ) ReactDom.createRoot(document.querySelector('#root')).render(VNode)
事件處理
注冊事件
語法:on+事件名={事件處理程序}
比如onClick={this.handleClick}
// 1. 導(dǎo)包 import React from "react" import ReactDom from 'react-dom/client' // 類組件 有狀態(tài) 如果有狀態(tài),狀態(tài)需要切換,更新視圖 用類組件 class App extends React.Component { // state節(jié)點(diǎn)中提供狀態(tài) 通過 this.state.xxx 來獲取狀態(tài) state = { name: 'Tt', age: 17 } // 提供一些方法 handleClick() { console.log('點(diǎn)擊'); } render() { return ( <div> <button onClick={this.handleClick}>按鈕</button> </div> ) } } const VNode = ( <div> <App></App> </div> ) ReactDom.createRoot(document.querySelector('#root')).render(VNode)
原文鏈接:https://blog.csdn.net/qq_41675812/article/details/128961155
相關(guān)推薦
- 2022-07-13 復(fù)選框 prop 不觸發(fā) change 事件
- 2022-05-15 實(shí)例詳解Python的進(jìn)程,線程和協(xié)程_python
- 2022-03-15 在Linux+Jexus中發(fā)布和部署Asp.Net?Core_自學(xué)過程
- 2022-05-27 yolov5訓(xùn)練時(shí)參數(shù)workers與batch-size的深入理解_python
- 2022-03-30 C#算法之回文數(shù)_C#教程
- 2022-05-11 springboot多版本管理
- 2022-06-09 FreeRTOS實(shí)時(shí)操作系統(tǒng)的任務(wù)通知方法_操作系統(tǒng)
- 2022-09-01 C#實(shí)現(xiàn)簡單工廠模式_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支