網站首頁 編程語言 正文
React三大屬性
props
- 組件是封閉的,接收外部數據應該通過props來實現
- 函數組件通過參數props來接收數據,props是一個對象; 類組件通過this.props接收數據。
- 傳遞數據:在組件標簽上添加屬性
函數組件
const Hello = (props) => { console.log(props); return (props:{props.name}) } ReactDOM.render(, document.getElementById('root'))
類組件
class App extends React.Component { render() { console.log(this.props); return (props: {this.props.name}) } } ReactDOM.render(, document.getElementById('root'))
在繼承類的構造函數中必須調用super函數,super代表父類的構造函數。ES6 要求,子類的構造函數必須執行一次super函數,否則會報錯。但是super函數內的this指向的是當前類的實例。
構造器是否接受 props,是否傳遞給 super,取決于是否希望在構造器中通過 this訪問props。
- 當構造器中接收了props參數,super沒有傳遞props,此時
this.props
是undefined
,當然可以正常使用props(前邊不加this) - 當構造器中接收了props參數,super也傳遞props,可以通過this.props拿到對象。
原因:類組件會繼承React.Component
,而Component的源碼是:
function Component(props, context, updater) { this.props = props; //綁定props this.context = context; //綁定context this.refs = emptyObject; //綁定ref this.updater = updater || ReactNoopUpdateQueue; //上面所屬的updater 對象 }
當我們在類組件中調用super,實際上是在執行父類的構造函數,如果沒有將props傳入super函數,那么在子類的構造函數中,this.props是undefined。
為什么仍然可以在 render和其他方法中訪問?this.props
。因為React 會在構造函數被調用之后,會把 props 賦值給剛剛創建的實例對象。
state
有狀態組件和無狀態組件
- 函數組件又叫做無狀態組件,函數組件沒有自己的狀態,只負責數據的靜態展示。
- 類組件又叫做有狀態組件,類組件有自己的狀態,負責更新UI,讓頁面動起來。
- 狀態(state)就是數據,是組件內部的私有數據,只能在組件內部使用。
每個組件都有state,它的值是對象類型;組件state屬性的初始化放在構造方法中。
class App extends React.Component { constructor() { super(); // 初始化state this.state = { count: 0 } } render() { return (計數器:{this.state.count}) } } ReactDOM.render(, document.getElementById('root'))
- 狀態是可變的:
this.setState({要修改的數據})
- 注意不要直接通過this.state.x = y 修改state中的數據,這是錯誤的寫法.
- etState的作用:
1. 修改state
2. 更新UI
setState
執行setState()的位置?
- 在react控制的回調函數中: (異步)
- 生命周期勾子 ,react事件監聽回調(合成事件)
- 非react控制的異步回調函數中: (同步)??????
- 定時器回調 ,原生事件監聽回調 , promise回調
setState是異步還是同步?
setState
的“異步”并不是說內部由異步代碼實現,其實本身執行的過程和代碼都是同步的。只是在 React 的?setState
?函數實現中,會根據?isBatchingUpdates
(默認是 false) 變量判斷是否直接更新?this.state
?還是放到隊列中稍后更新。然后有一個?batchedUpdate
?函數,可以修改?isBatchingUpdates
?為 true,當 React 調用事件處理函數之前,或者生命周期函數之前就會調用?batchedUpdate
?函數,這樣的話,setState 就不會同步更新 this.state,而是放到更新隊列里面后續更新。
這樣你就可以理解為什么原生事件和?setTimeout/setinterval
?里面調用 this.state 會同步更新了,因為通過這些函數調用的 React?沒辦法去調用 batchedUpdate 函數將 isBatchingUpdates 設置為 true,那么這個時候 setState 的時候默認就是 false,那么就會同步更新。
props和state屬性的區別
props 是組件對外的接口,state 是組件對內的接口。
主要區別:
- State是可變的,是一組用于反映組件UI變化的狀態集合。
- 而Props對于使用它的組件來說,是只讀的,要想修改Props,只能通過該組件的父組件修改。
refs
refs是彈性文件系統,在React中可以獲取React元素或DOM元素。
?我們在日常寫React代碼的時候,一般情況是用不到Refs這個東西,因為我們并不直接操作底層DOM元素,而是在render函數里去編寫我們的頁面結構,由React來組織DOM元素的更新。凡事總有例外,總會有一些很奇葩的時候我們需要直接去操作頁面的真實DOM,這就要求我們有直接訪問真實DOM的能力,而Refs就是為我們提供了這樣的能力。
React.createRef
React.createRef調用后可以返回一個容器,該容器可以存儲被ref所標識的節點,該容器是專人專用的:
class App extends React.Component { inputRef = React.createRef(); showData = () => { let input = this.inputRef.current; console.log(input.value) } render() { return () } }
ref的綁定
class Person extends React.Component{ constructor(){ super(); this.handleClick = this.handleClick.bind(this); } handleClick(){ // 使用原生的 DOM API 獲取焦點 this.refs.myInput.focus(); } render(){ return (); } } ReactDOM.render(, document.getElementById('root'));
- 當 ref 屬性用于?HTML 元素時,使用 React.createRef()或者React.useRef() 創建的 ref 接收底層 DOM 元素作為其 current 屬性。
- 當 ref 屬性用于?class 組件時,ref 對象接收組件的掛載實例作為其 current 屬性。
- 你不能在函數組件上使用 ref 屬性,因為他們沒有實例。函數組件可以使用useRef(),它所返回的對象在組件的整個生命周期內不變。
總結
原文鏈接:https://blog.csdn.net/Darlingmi/article/details/123163547
相關推薦
- 2022-08-19 關于?React?中?useEffect?使用問題淺談_React
- 2022-04-30 .Net?生成壓縮文件問題記錄(推薦)_實用技巧
- 2022-08-06 C語言實現簡單登錄操作_C 語言
- 2022-09-27 C#中的const和readonly關鍵字詳解_C#教程
- 2022-09-24 Golang?斷言與閉包使用解析_Golang
- 2022-01-01 使用el-date-picker根據開始月份,動態禁用結束月份
- 2022-11-25 淺析Golang中的內存逃逸_Golang
- 2022-08-16 C#?泛型List排序的實現_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同步修改后的遠程分支