網站首頁 編程語言 正文
項目場景:
使用過vue的朋友都知道,vue父組件更改props的值,子組件是會刷新的,而react就未必。
先看以下例子:
1、創建父組件
antd-mobile依賴需要自行引入
export default class Parent2 extends Component {
constructor(props){
super(props)
//初始化state
this.state={
parentVal: 10,
}
}
count=1
handleClick=()=>{
this.setState({
parentVal:this.state.parentVal+1
})
}
changeCount=()=>{
this.count=this.count+1
console.log(this.count)
}
componentDidMount() {
console.log("父組件生命周期======:Mount")
}
//掛載完成后更新狀態值,render()結束后會執行componentDidUpdate()鉤子函數
componentDidUpdate(prevProps, prevState, snapshot) {
console.log("父組件生命周期======:Update")
}
componentWillUnmount() {
console.log("父組件生命周期======:Unmount")
}
render() {
console.log("父組件生命周期======:render")
return (
<div>
父組件
<p>{this.state.parentVal}</p>
<Divider>分割線</Divider>
<Button color='primary' size='small' onClick={this.handleClick}>更改state的值</Button>
<p>父組件調用setState()時,子組件也會執行render()方法,</p>
<Button color='primary' size='small' onClick={this.changeCount}>更改count的值</Button>
<Child2 number={this.state.parentVal} count={this.count}/>
</div>
)
}
}
2、創建子組件
export default class Child2 extends Component {
constructor(props){
super(props)
}
componentDidMount() {
console.log("子組件生命周期======:Mount")
}
//掛載完成后更新狀態值,render()結束后會執行componentDidUpdate()鉤子函數
componentDidUpdate(prevProps, prevState, snapshot) {
console.log("子組件生命周期======:Update")
}
componentWillUnmount() {
console.log("子組件生命周期======:Unmount")
}
render() {
console.log("子組件生命周期======:render")
return (
<div style={{background:'yellow',padding:20}}>
<p>父組件state中傳值:{this.props.number}</p>
<p>父組件非state中傳值:{this.props.count}</p>
</div>
)
}
}
問題描述
這里有兩個變量,一個count,不是父組件state中的值,一個numer是父組件state中的值。
點擊更改‘更改state的值’按鈕,父子組件同步刷新,而點擊更改count的值按鈕,子組件不會刷新
原因分析:
父組件更改count的值,雖然父組件count值改變,但是不會更改子組件的值,props是單向傳遞的
情況組件掛載生命周期鉤子函數執行控制臺打印數據
父組件執行setState()結果:
- 要想讓子組件更新dom,必須讓子組件執行render()方法,而父組件執行render()方法后,子組件也會執行render()方法,這就是為何父組件調用了setSate()方法,子組件會刷新。
- 當更改了count的值,比如count連續加1,變成了9,此時父組件調用this.setState()更改狀態值,
此時子組件的count也變成了9,因為count并沒有清除,父子組件又先后調用了render()方法,因此渲染上了最新的props的屬性值
如果子組件是函數組件,則render后,count值會變為初始值,那么父組件setState()之后,子組件render()函數執行時收到的還是最初的值,這和子組件是類組件有區別,大家可以自己嘗試,
解決方案:
如果想要傳遞子組件的props改變后刷新子組件dom,就要將父組件的state中的值傳給子組件,而不是將普通的變量傳遞給子組件
vue更改props的值子組件會刷新,因為vue中傳遞給props的值也是父組件狀態中的變量
原文鏈接:https://blog.csdn.net/lvfengxian2019/article/details/126724921
相關推薦
- 2022-09-07 如何應用?SOLID?原則在?React?中整理代碼之開閉原則_React
- 2023-02-03 C++中為何推薦要把基類析構函數設置成虛函數_C 語言
- 2022-02-02 appname is automatically signed for development,
- 2022-03-30 C語言關鍵字之auto?register詳解_C 語言
- 2022-05-11 垃圾收集器ParNew&CMS與底層三色標記算法詳解
- 2022-09-24 python基于numpy的線性回歸_python
- 2022-02-25 antd4中Form.create已廢棄
- 2022-07-30 if-else和switch的練習及區別比較
- 最近更新
-
- 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同步修改后的遠程分支