網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Props使用
Props的基本使用
-
props傳遞接受數(shù)據(jù)
???1.在組件標(biāo)簽上添加屬性
????2.類組件使用 this.props 接收數(shù)據(jù)。函數(shù)組件使用參數(shù) props 接收數(shù)據(jù)。
- 使用類接收數(shù)據(jù)
// 類接受 props 數(shù)據(jù) class App extends React.Component { render() { return ( <div> <h1>姓名:{this.props.name}</h1> <h1>年齡:{this.props.age}</h1> </div> ) } } ReactDOM.render(<App name="Tom" age="18" />, document.getElementById('root'))
- 使用函數(shù)接收數(shù)據(jù)
// 函數(shù)接收 props 數(shù)據(jù) const App = props => { return ( <div> <h1>姓名:{props.name}</h1> <h1>年齡:{props.age}</h1> </div> ) } ReactDOM.render(<App name="Tom" age="18" />, document.getElementById('root'))
-
props 特點(diǎn)
-
可以給組件傳遞任意類型的數(shù)據(jù)
-
props 傳遞的數(shù)據(jù)是只讀的數(shù)據(jù)。只能讀取屬性的值,而不能進(jìn)行修改。
-
在使用類組件時(shí),若寫了構(gòu)造函數(shù),必須將 props 傳給 super()
-
constructor(props) {
super(props)
...
}
Props深入
-
Children屬性:
-
表示組件的子節(jié)點(diǎn)。有子節(jié)點(diǎn)的時(shí)候, Props 就會(huì)有這個(gè)屬性
-
和普通的Props 一樣,值可以是任意值
-
-
Props的校驗(yàn):
-
對(duì)于組件來(lái)說(shuō) Props 是外來(lái)的,無(wú)法保證組件的使用者傳入的數(shù)據(jù)格式
-
傳入的數(shù)據(jù)格式不對(duì)的話,會(huì)導(dǎo)致組件內(nèi)部報(bào)錯(cuò),且不知道明確的錯(cuò)誤原因
-
Props校驗(yàn):在創(chuàng)建組件的時(shí)候,會(huì)指定 Props 的類型和格式---這樣就算格式出錯(cuò)也會(huì)給出一個(gè)明確的錯(cuò)誤。
-
使用步驟:
-
安裝 Props-types(npm i prop-types/yarn add prop-types)
-
導(dǎo)入prop-types 包
-
使用 組件名.propType = {} 給組件添加校驗(yàn)規(guī)則
-
-
```javascript
import propTypes from 'prop-types'
function App(props) {
return (
<h1>{props.name}</h1>
)
}
App.propTypes = {
name : PropTypes.string
}
```
-
約束規(guī)則
-
常見類型: Array、number、object、string、bool、func
-
React 元素類型: element
-
是否必填:isRequired
-
特定結(jié)構(gòu)的對(duì)象: shape({ }) ----- 約束多個(gè)屬性的時(shí)候,整合到這個(gè)對(duì)象中一起書寫
-
-
Props的默認(rèn)值
- 定義一個(gè)默認(rèn)值,不傳入數(shù)值的時(shí)候,默認(rèn)采用這個(gè)數(shù)值
const App = props => { return ( <div>props.name/div> ) } //添加默認(rèn)值 App.defaultProps = { name : 'Tom' }
原文鏈接:https://blog.csdn.net/weixin_51642358/article/details/126414438
- 上一篇:存儲(chǔ)引擎的應(yīng)用場(chǎng)景
- 下一篇:React組件通信
相關(guān)推薦
- 2022-11-08 詳解Python中數(shù)據(jù)處理的方法總結(jié)及實(shí)現(xiàn)_python
- 2022-12-13 iOS底層實(shí)例解析Swift閉包及OC閉包_IOS
- 2023-02-06 C#實(shí)現(xiàn)獲取機(jī)器碼的示例詳解_C#教程
- 2022-09-15 Pycharm虛擬環(huán)境創(chuàng)建并使用命令行指定庫(kù)的版本進(jìn)行安裝_python
- 2022-03-26 c#使用listbox的詳細(xì)方法和常見問(wèn)題解決_C#教程
- 2022-11-30 Git基礎(chǔ)學(xué)習(xí)之分支基本操作詳解_相關(guān)技巧
- 2022-07-20 Python3.7.2環(huán)境安裝
- 2022-07-11 pandas如何計(jì)算同比環(huán)比增長(zhǎng)_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- 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)證過(guò)濾器
- 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)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支