React 原理詳解
1.setState() 說(shuō)明
1.1 更新數(shù)據(jù)
setState() 是異步更新數(shù)據(jù)
可以多次調(diào)用 setState() ,只會(huì)觸發(fā)一次重新渲染
import React from 'react'
import ReactDOM from 'react-dom'
class Opp extends React.Component {
state = {
count: 1,
}
handleClick = () => {
// 異步更新數(shù)據(jù)
this.setState({
count: this.state.count + 1,
})
this.setState({
count: this.state.count + 1,
})
console.log(this.state.count) // 1
}
render() {
return (
<div>
<h1>計(jì)數(shù)器:{this.state.count}</h1>
<button onClick={this.handleClick}>+1</button>
</div>
)
}
}
ReactDOM.render(<Opp />, document.getElementById('root'))
1.2 推薦語(yǔ)法
使用 setState((state,props)=>{}) 語(yǔ)法
state:表示最新的 state, props:表示最新的 props
import React from 'react'
import ReactDOM from 'react-dom'
class Opp extends React.Component {
state = {
count: 1,
}
handleClick = () => {
/* // 異步更新數(shù)據(jù)
this.setState({
count: this.state.count + 1,
})
console.log(this.state.count) //1
this.setState({
count: this.state.count + 1,
})
console.log(this.state.count) //1
*/
// 推薦語(yǔ)法
this.setState((state, props) => {
return {
count: state.count + 1,
}
})
this.setState((state, props) => {
console.log('第二次調(diào)用:', state) //2
return {
count: state.count + 1,
}
})
console.log(this.state.count) // 3
}
render() {
return (
<div>
<h1>計(jì)數(shù)器:{this.state.count}</h1>
<button onClick={this.handleClick}>+1</button>
</div>
)
}
}
ReactDOM.render(<Opp />, document.getElementById('root'))
1.3 第二個(gè)參數(shù)
- 在狀態(tài)更新(頁(yè)面完成重新渲染)后立即執(zhí)行某個(gè)操作
- 語(yǔ)法:setState(updater[,callback])
callback 是指回調(diào)函數(shù) 可加可不加
import React from 'react'
import ReactDOM from 'react-dom'
class Opp extends React.Component {
state = {
count: 1,
}
handleClick = () => {
this.setState(
(state, props) => {
return {
count: state.count + 1,
}
},
// 狀態(tài)更新后并且重新渲染后,立即執(zhí)行
() => {
console.log('狀態(tài)更新完成:', this.state.count) // 2
console.log(document.getElementById('title').innerText) // 計(jì)數(shù)器:2
document.title = '更新后的 count 為:' + this.state.count
}
)
console.log(this.state.count) //1
}
render() {
return (
<div>
<h1 id='title'>計(jì)數(shù)器:{this.state.count}</h1>
<button onClick={this.handleClick}>+1</button>
</div>
)
}
}
ReactDOM.render(<Opp />, document.getElementById('root'))
2.JSX 語(yǔ)法的轉(zhuǎn)化過(guò)程
- JSX 僅僅是 createElement() 方法的語(yǔ)法糖(簡(jiǎn)化語(yǔ)法)
- JSX 語(yǔ)法被 @babel/preset-react 插件編譯為 createElement() 方法
- React元素:是一個(gè)對(duì)象,用來(lái)描述你希望在屏幕上看到的內(nèi)容

import React from 'react'
import ReactDOM from 'react-dom'
// JSX 語(yǔ)法的轉(zhuǎn)化過(guò)程
// const element = <h1 className='greeting'>Hello JSX</h1>
const element = React.createElement(
'h1',
{
className: 'greeting',
},
'Hello JSX'
)
console.log(element)
ReactDOM.render(element, document.getElementById('root'))
3.組件更新機(jī)制
- setState() 的兩個(gè)作用:1.修改 state 2.更新組件(UI)
- 過(guò)程:父組件重新渲染是,也會(huì)重新渲染子組件,但只會(huì)渲染當(dāng)前組件子樹(當(dāng)前組件及其所有子組件)

4.組件性能優(yōu)化
4.1 減輕 state
- 減輕 state :只存儲(chǔ)跟組件渲染相關(guān)的數(shù)據(jù)(比如:count /列表數(shù)據(jù)/ loading 等)
- 注意:不用渲染的書籍不要放在 state 中(比如定時(shí)器 id 等)
- 需要在多個(gè)方法中用到的數(shù)據(jù),應(yīng)該放在 this 中
4.2 避免不必要的重新渲染
- 組件更新機(jī)制:父組件更新會(huì)引起子組件也被更新
- 問(wèn)題:子組件沒有變化時(shí)也會(huì)被重新渲染,造成不必要的重新渲染
- 解決方式:使用鉤子函數(shù)shouldComponentUpdate(nextProps,nextState)
- 作用:通過(guò)返回值決定該組件是否重新渲染,返回 true 表示重新渲染, false 表示不重新渲染
- 觸發(fā)時(shí)機(jī):更新階段的鉤子函數(shù),組件重新渲染前執(zhí)行(shouldComponentUpdate -> render)
import React from 'react'
import ReactDOM from 'react-dom'
class Opp extends React.Component {
state = {
count: 0,
}
handleClick = () => {
this.setState((state) => {
return {
count: this.state.count + 1,
}
})
}
// 鉤子函數(shù)
shouldComponentUpdate(nextProps, nextState) {
// 返回 false,阻止組件重新渲染
// return false
// 最新的狀態(tài)
console.log('最新的state', nextState)
// 更新前的狀態(tài)
console.log(this.state)
// 返回 true,組件重新渲染
return true
}
render() {
console.log('組件更新了')
return (
<div>
<h1>計(jì)數(shù)器:{this.state.count}</h1>
<button onClick={this.handleClick}>+1</button>
</div>
)
}
}
ReactDOM.render(<Opp />, document.getElementById('root'))
案例:隨機(jī)數(shù)
通過(guò) nextState
import React from 'react'
import ReactDOM from 'react-dom'
// 生成隨機(jī)數(shù)
class Opp extends React.Component {
state = {
number: 0,
}
handleClick = () => {
this.setState((state) => {
return {
number: Math.floor(Math.random() * 3),
}
})
}
// 兩次生成的隨機(jī)數(shù)可能相同,則沒必要重新渲染
shouldComponentUpdate(nextState) {
console.log('最新狀態(tài):', nextState, '當(dāng)前狀態(tài):', this.state)
return nextState.number !== this.state.number
/* if ( nextState.number !== this.state.number) {
return true
}
return false*/
}
render() {
console.log('render')
return (
<div>
<h1>隨機(jī)數(shù):{this.state.number}</h1>
<button onClick={this.handleClick}>重新生成</button>
</div>
)
}
}
ReactDOM.render(<Opp />, document.getElementById('root'))
通過(guò) nextState
import React from 'react'
import ReactDOM from 'react-dom'
// 生成隨機(jī)數(shù)
class Opp extends React.Component {
state = {
number: 0,
}
handleClick = () => {
this.setState((state) => {
return {
number: Math.floor(Math.random() * 3),
}
})
}
render() {
return (
<div>
<NumberBox number={this.state.number} />
<button onClick={this.handleClick}>重新生成</button>
</div>
)
}
}
class NumberBox extends React.Component {
shouldComponentUpdate(nextProps) {
console.log('最新props:', nextProps, '當(dāng)前props:', this.props)
return nextProps.number !== this.props.number
}
render() {
console.log('子組件render')
return <h1>隨機(jī)數(shù):{this.props.number}</h1>
}
}
ReactDOM.render(<Opp />, document.getElementById('root'))
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
React中使用react-json-view展示JSON數(shù)據(jù)的操作方法
react-json-view是一個(gè)用于顯示和編輯javascript數(shù)組和JSON對(duì)象的React組件,本文給大家分享React中使用react-json-view展示JSON數(shù)據(jù)的操作方法,感興趣的朋友一起看看吧2023-12-12
React組件三大核心屬性State?props?Refs介紹
組件實(shí)例的三大核心屬性是:State、Props、Refs。類組件中這三大屬性都存在。函數(shù)式組件中訪問(wèn)不到?this,也就不存在組件實(shí)例這種說(shuō)法,但由于它的特殊性(函數(shù)可以接收參數(shù)),所以存在Props這種屬性2023-02-02
React從react-router路由上做登陸驗(yàn)證控制的方法
本篇文章主要介紹了React從react-router路由上做登陸驗(yàn)證控制的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
使用React實(shí)現(xiàn)一個(gè)簡(jiǎn)單的待辦事項(xiàng)列表的示例代碼
這篇文章我們將詳細(xì)講解如何建立一個(gè)這樣簡(jiǎn)單的列表,文章通過(guò)代碼示例介紹的非常詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08
React中事件綁定this指向三種方法的實(shí)現(xiàn)
這篇文章主要介紹了React中事件綁定this指向三種方法的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
React項(xiàng)目中使用zustand狀態(tài)管理的實(shí)現(xiàn)
zustand是一個(gè)用于狀態(tài)管理的小巧而強(qiáng)大的庫(kù),本文主要介紹了React項(xiàng)目中使用zustand狀態(tài)管理的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
React中使用TS完成父組件調(diào)用子組件的操作方法
由于在項(xiàng)目開發(fā)過(guò)程中,我們往往時(shí)需要調(diào)用子組件中的方法,這篇文章主要介紹了React中使用TS完成父組件調(diào)用子組件,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
react-native 封裝視頻播放器react-native-video的使用
本文主要介紹了react-native 封裝視頻播放器react-native-video的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

