React 原理詳解
更新時間:2021年10月10日 11:38:46 作者:keinee
這篇文章主要介紹了深入理解react的原理,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
1.setState() 說明
1.1 更新數據
setState() 是異步更新數據
可以多次調用 setState() ,只會觸發(fā)一次重新渲染
import React from 'react'
import ReactDOM from 'react-dom'
class Opp extends React.Component {
state = {
count: 1,
}
handleClick = () => {
// 異步更新數據
this.setState({
count: this.state.count + 1,
})
this.setState({
count: this.state.count + 1,
})
console.log(this.state.count) // 1
}
render() {
return (
<div>
<h1>計數器:{this.state.count}</h1>
<button onClick={this.handleClick}>+1</button>
</div>
)
}
}
ReactDOM.render(<Opp />, document.getElementById('root'))
1.2 推薦語法
使用 setState((state,props)=>{}) 語法
state:表示最新的 state, props:表示最新的 props
import React from 'react'
import ReactDOM from 'react-dom'
class Opp extends React.Component {
state = {
count: 1,
}
handleClick = () => {
/* // 異步更新數據
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
*/
// 推薦語法
this.setState((state, props) => {
return {
count: state.count + 1,
}
})
this.setState((state, props) => {
console.log('第二次調用:', state) //2
return {
count: state.count + 1,
}
})
console.log(this.state.count) // 3
}
render() {
return (
<div>
<h1>計數器:{this.state.count}</h1>
<button onClick={this.handleClick}>+1</button>
</div>
)
}
}
ReactDOM.render(<Opp />, document.getElementById('root'))
1.3 第二個參數
- 在狀態(tài)更新(頁面完成重新渲染)后立即執(zhí)行某個操作
- 語法:setState(updater[,callback])
callback 是指回調函數 可加可不加
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) // 計數器:2
document.title = '更新后的 count 為:' + this.state.count
}
)
console.log(this.state.count) //1
}
render() {
return (
<div>
<h1 id='title'>計數器:{this.state.count}</h1>
<button onClick={this.handleClick}>+1</button>
</div>
)
}
}
ReactDOM.render(<Opp />, document.getElementById('root'))
2.JSX 語法的轉化過程
- JSX 僅僅是 createElement() 方法的語法糖(簡化語法)
- JSX 語法被 @babel/preset-react 插件編譯為 createElement() 方法
- React元素:是一個對象,用來描述你希望在屏幕上看到的內容

import React from 'react'
import ReactDOM from 'react-dom'
// JSX 語法的轉化過程
// 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.組件更新機制
- setState() 的兩個作用:1.修改 state 2.更新組件(UI)
- 過程:父組件重新渲染是,也會重新渲染子組件,但只會渲染當前組件子樹(當前組件及其所有子組件)

4.組件性能優(yōu)化
4.1 減輕 state
- 減輕 state :只存儲跟組件渲染相關的數據(比如:count /列表數據/ loading 等)
- 注意:不用渲染的書籍不要放在 state 中(比如定時器 id 等)
- 需要在多個方法中用到的數據,應該放在 this 中
4.2 避免不必要的重新渲染
- 組件更新機制:父組件更新會引起子組件也被更新
- 問題:子組件沒有變化時也會被重新渲染,造成不必要的重新渲染
- 解決方式:使用鉤子函數shouldComponentUpdate(nextProps,nextState)
- 作用:通過返回值決定該組件是否重新渲染,返回 true 表示重新渲染, false 表示不重新渲染
- 觸發(fā)時機:更新階段的鉤子函數,組件重新渲染前執(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,
}
})
}
// 鉤子函數
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>計數器:{this.state.count}</h1>
<button onClick={this.handleClick}>+1</button>
</div>
)
}
}
ReactDOM.render(<Opp />, document.getElementById('root'))
案例:隨機數
通過 nextState
import React from 'react'
import ReactDOM from 'react-dom'
// 生成隨機數
class Opp extends React.Component {
state = {
number: 0,
}
handleClick = () => {
this.setState((state) => {
return {
number: Math.floor(Math.random() * 3),
}
})
}
// 兩次生成的隨機數可能相同,則沒必要重新渲染
shouldComponentUpdate(nextState) {
console.log('最新狀態(tài):', nextState, '當前狀態(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>隨機數:{this.state.number}</h1>
<button onClick={this.handleClick}>重新生成</button>
</div>
)
}
}
ReactDOM.render(<Opp />, document.getElementById('root'))
通過 nextState
import React from 'react'
import ReactDOM from 'react-dom'
// 生成隨機數
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, '當前props:', this.props)
return nextProps.number !== this.props.number
}
render() {
console.log('子組件render')
return <h1>隨機數:{this.props.number}</h1>
}
}
ReactDOM.render(<Opp />, document.getElementById('root'))
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!
相關文章
React中使用react-json-view展示JSON數據的操作方法
react-json-view是一個用于顯示和編輯javascript數組和JSON對象的React組件,本文給大家分享React中使用react-json-view展示JSON數據的操作方法,感興趣的朋友一起看看吧2023-12-12
React組件三大核心屬性State?props?Refs介紹
組件實例的三大核心屬性是:State、Props、Refs。類組件中這三大屬性都存在。函數式組件中訪問不到?this,也就不存在組件實例這種說法,但由于它的特殊性(函數可以接收參數),所以存在Props這種屬性2023-02-02
React從react-router路由上做登陸驗證控制的方法
本篇文章主要介紹了React從react-router路由上做登陸驗證控制的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
react-native 封裝視頻播放器react-native-video的使用
本文主要介紹了react-native 封裝視頻播放器react-native-video的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01

