React的Props、生命周期詳解
Props 的只讀性
“Props” 是 React 中用于傳遞數(shù)據(jù)給組件的一種機制,通常作為組件的參數(shù)進行傳遞。在 React 中,props 是只讀的,意味著一旦將數(shù)據(jù)傳遞給組件的 props,組件就不能直接修改這些 props 的值。所以組件無論是使用函數(shù)聲明還是通過 class 聲明,都決不能修改自身的 props。
所以React有一個嚴格的規(guī)則:所有 React 組件都必須像純函數(shù)一樣保護它們的 props 不被更改。
為什么Props是只讀的呢?
當我們在父組件中將數(shù)據(jù)傳遞給子組件時,子組件只能使用這些 props 來讀取數(shù)據(jù),而不能修改它們。這是為了確保數(shù)據(jù)的單向流動,使得數(shù)據(jù)的流動更加可控和可預測。當 Props 是只讀的時候,我們可以確保數(shù)據(jù)只能從父組件流向子組件,而子組件不能直接修改父組件傳遞的數(shù)據(jù)。這種單向數(shù)據(jù)流有助于維護組件的可預測性和代碼的可維護性。
如果我無法避免要在組件內部修改數(shù)據(jù),該怎么辦?
如果你需要在組件內部修改數(shù)據(jù),你可以使用組件的狀態(tài)(state)。狀態(tài)是組件內部的可變數(shù)據(jù),可以通過特定的方法來修改。但是這些狀態(tài)無法直接傳遞給其他組件,如果需要在多個組件之間共享數(shù)據(jù),可以考慮使用上層組件的狀態(tài)或者全局狀態(tài)管理工具(如 Redux)
代碼示例:
import React, { useState } from 'react';
function ParentComponent() {
const [count, setCount] = useState(0);
const incrementCount = () => {
setCount(count + 1);
};
return (
<div>
<h2>父組件</h2>
<p>Count: {count}</p>
<ChildComponent count={count} increment={incrementCount} />
</div>
);
}
function ChildComponent(props) {
return (
<div>
<h2>子組件t</h2>
<p>總和: {props.count}</p>
<button onClick={props.increment}>+1</button>
</div>
);
}
export default ParentComponent;如何將函數(shù)組件轉換成 class 組件 創(chuàng)建一個同名的 ES6 class,并且繼承于 React.Component。
- 添加一個空的
render()方法。 - 將函數(shù)體移動到
render()方法之中。 - 在
render()方法中使用this.props替換props。 - 刪除剩余的空函數(shù)聲明。
函數(shù)式組件
function tick(Props) {
const element = (
<div>
<h1>Hello, world!</h1>
<h2>It is {Props.time.toLocaleTimeString()}.</h2>
</div>
);
root.render(element);
}class組件
class Clock extends React.Component {
render() {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.props.date.toLocaleTimeString()}.</h2>
</div>
);
}
}生命周期
掛載
- constructor--------組件實例化時執(zhí)行,用于初始化state和綁定事件等操作
- getDerivedStateFromProps --------在render方法執(zhí)行之前調用,用于根據(jù)props設置state。
- render--------渲染組件
- componentDidMount(-------組件掛載到DOM后執(zhí)行,用于執(zhí)行一些需要DOM的操作,如獲取數(shù)據(jù)。
更新
- getDerivedStateFromProps-------在render方法執(zhí)行之前調用,用于根據(jù)props設置state
- shouldComponentUpdate------判斷組件是否需要重新渲染,默認返回true
- render------渲染組件
- getSnapshotBeforeUpdate------在更新前獲取DOM信息,如滾動位置等。
- componentDidUpdate--------組件更新后執(zhí)行,用于執(zhí)行一些需要DOM的操作,如更新數(shù)據(jù)
卸載
- componentWillUnmount------組件從DOM中移除時經歷的階段

寫一個時鐘案例,每秒都會更新時間
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {date: new Date()};
}
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick() {
this.setState({
date: new Date()
});
}
render() {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Clock />);讓我們來快速概括一下發(fā)生了什么和這些方法的調用順序:
- 當
<Clock />被傳給root.render()的時候,React 會調用Clock組件的構造函數(shù)。因為 Clock 需要顯示當前的時間,所以它會用一個包含當前時間的對象來初始化this.state。我們會在之后更新 state。 - 之后 React 會調用組件的
render()方法。這就是 React 確定該在頁面上展示什么的方式。然后 React 更新 DOM 來匹配Clock渲染的輸出。 - 當
Clock的輸出被插入到 DOM 中后,React 就會調用ComponentDidMount()生命周期方法。在這個方法中,Clock 組件向瀏覽器請求設置一個計時器來每秒調用一次組件的tick()方法。 - 瀏覽器每秒都會調用一次
tick()方法。 在這方法之中,Clock組件會通過調用setState()來計劃進行一次 UI 更新。得益于setState()的調用,React 能夠知道state已經改變了,然后會重新調用render()方法來確定頁面上該顯示什么。這一次,render()方法中的this.state.date就不一樣了,如此一來就會渲染輸出更新過的時間。 - React 也會相應的更新 DOM。一旦 Clock 組件從 DOM 中被移除,React 就會調用
componentWillUnmount()生命周期方法,這樣計時器就停止了。
到此這篇關于React的Props、生命周期的文章就介紹到這了,更多相關React的Props、生命周期內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Webpack+Babel+React開發(fā)環(huán)境的搭建的方法步驟
本篇文章主要介紹了詳解Webpack+Babel+React開發(fā)環(huán)境的搭建的方法步驟,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
詳解React Native網(wǎng)絡請求fetch簡單封裝
本篇文章主要介紹了詳解React Native網(wǎng)絡請求fetch簡單封裝,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
React Router中Link和NavLink的學習心得總結
這篇文章主要介紹了React Router中Link和NavLink的學習心得總結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12

