網站首頁 編程語言 正文
一、props規則校驗
安裝 prop-types 包
$ npm install prop-types
導入 propTypes 對象
import propTypes from 'prop-types';
組件名.propTypes = {} 設置組件 傳參規則
Comp.propTypes = { param: propTypes.array // Comp組件 的 param 參數必須是 數組類型 }
示例:
// props 類型校驗規則 import React from 'react'; // 1. npm i prop-types // 2. 導入 propTypes 對象 import PropTypes from "prop-types"; function Son({list}) { return ( <div> {list.map(item => <p key={item}>{item}</p>)} </div> ) } // 3. 組件名.propTypes = {} 給組件設置規則 Son.PropTypes={ // 4. 各字段設置規則 list: PropTypes.array // Son的list參數必須是 數組形式 } class App extends React.Component { render() { return ( <div> <Son list="我企鵝親子裝"/> </div> ) } } export default App;
報錯提示如下:
四種常見結構
- 常用類型:
array
、number
、bool
、string
、func
、object
、symbol
- React元素類型:
element
- 必填項:
isRequired
- 特定的結構對象:
shape({})
核心代碼:
// 1.類型 optionalFun: PropTypes.fun; // 2.必填項 requiredFun: PropTypes.fun.isRequired; // 3. // 可以指定一個對象由特定的類型值組成 optionalObjectWithShape: PropTypes.shape({ color: PropTypes.string, fontSize: PropTypes.number }),
二、props默認值
1.函數式默認值
1.1?函數參數默認值(新版推薦)
示例:
import React from "react"; // 1. 函數參數默認值 function Son1({defaultTime = 10}) { return ( <div>The timer is : {defaultTime}</div> ) } class App extends React.Component { render() { return ( <div> <Son1 /> </div> ) } } export default App;
1.2?defaultProps?設置默認值
function Son2({defaultTime}) { return ( <div>The second timer is: {defaultTime}</div> ) } // 2. defaultProps 設置默認值 Son2.defaultProps = { defaultTime: 100 } class App extends React.Component { render() { return ( <div> <Son1 /> <Son2 /> </div> ) } }
2.類式默認值
2.1?defaultProps
class Son3 extends React.Component { render() { return ( <div>The defaultTimer is : {this.props.defaultTime}</div> ) } } // defaultProps 設置默認值 Son3.defaultProps = { defaultTime: 3333 }
2.2?類靜態屬性聲明
class Son4 extends React.Component { static defaultProps ={ defaultTime: 66666 } render() { return ( <div>The defaultTimer is : {this.props.defaultTime}</div> ) } }
完整示例
// props默認值 import { func } from "prop-types"; import React from "react"; // 1.1 函數參數默認值 function Son1({defaultTime = 10}) { return ( <div>The timer is : {defaultTime}</div> ) } function Son2({defaultTime}) { return ( <div>The second timer is: {defaultTime}</div> ) } // 1.2 defaultProps 設置默認值 Son2.defaultProps = { defaultTime: 100 } class Son3 extends React.Component { render() { return ( <div>The defaultTimer is : {this.props.defaultTime}</div> ) } } // 2.1 函數 defaultProps 設置默認值 Son3.defaultProps = { defaultTime: 3333 } // 2.2 靜態屬性聲明 class Son4 extends React.Component { static defaultProps ={ defaultTime: 66666 } render() { return ( <div>The defaultTimer is : {this.props.defaultTime}</div> ) } } class App extends React.Component { render() { return ( <div> <Son1 /> <Son2 /> <Son3 /> <Son4 /> </div> ) } } export default App;
如圖:
總結
原文鏈接:https://blog.csdn.net/RedaTao/article/details/123826407
相關推薦
- 2022-08-04 Android學習之Span的使用方法詳解_Android
- 2022-06-12 IDEA如何配置Tomcat
- 2023-02-23 Android中URLEncoder空格被轉碼為"+"號的處理辦法_Android
- 2022-08-15 el-table表格怎么在表頭的某項添加提示信息
- 2023-11-12 python enumerate函數用法
- 2022-06-17 Ruby操作CSV格式數據方法詳解_ruby專題
- 2022-01-29 寶塔部署Yii框架多個商城項目,隊列問題“服務測試失敗,請檢查服務是否正常運行”
- 2022-12-07 C++中的類成員函數當線程函數_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支