react父組件更改props子組件無法刷新原因及解決方法
項目場景:
使用過vue的朋友都知道,vue父組件更改props的值,子組件是會刷新的,而react就未必。
先看以下例子:
1、創(chuàng)建父組件
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") } //掛載完成后更新狀態(tài)值,render()結(jié)束后會執(zhí)行componentDidUpdate()鉤子函數(shù) 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>父組件調(diào)用setState()時,子組件也會執(zhí)行render()方法,</p> <Button color='primary' size='small' onClick={this.changeCount}>更改count的值</Button> <Child2 number={this.state.parentVal} count={this.count}/> </div> ) } }
2、創(chuàng)建子組件
export default class Child2 extends Component { constructor(props){ super(props) } componentDidMount() { console.log("子組件生命周期======:Mount") } //掛載完成后更新狀態(tài)值,render()結(jié)束后會執(zhí)行componentDidUpdate()鉤子函數(shù) 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是單向傳遞的
情況組件掛載生命周期鉤子函數(shù)執(zhí)行控制臺打印數(shù)據(jù)
父組件執(zhí)行setState()結(jié)果:
- 要想讓子組件更新dom,必須讓子組件執(zhí)行render()方法,而父組件執(zhí)行render()方法后,子組件也會執(zhí)行render()方法,這就是為何父組件調(diào)用了setSate()方法,子組件會刷新。
- 當更改了count的值,比如count連續(xù)加1,變成了9,此時父組件調(diào)用this.setState()更改狀態(tài)值,
此時子組件的count也變成了9,因為count并沒有清除,父子組件又先后調(diào)用了render()方法,因此渲染上了最新的props的屬性值
如果子組件是函數(shù)組件,則render后,count值會變?yōu)槌跏贾?,那么父組件setState()之后,子組件render()函數(shù)執(zhí)行時收到的還是最初的值,這和子組件是類組件有區(qū)別,大家可以自己嘗試,
解決方案:
如果想要傳遞子組件的props改變后刷新子組件dom,就要將父組件的state中的值傳給子組件,而不是將普通的變量傳遞給子組件
vue更改props的值子組件會刷新,因為vue中傳遞給props的值也是父組件狀態(tài)中的變量
到此這篇關于react父組件更改props子組件無法刷新原因的文章就介紹到這了,更多相關react父組件更改props子組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
create-react-app使用antd按需加載的樣式無效問題的解決
這篇文章主要介紹了create-react-app使用antd按需加載的樣式無效問題的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02React實現(xiàn)控制減少useContext導致非必要的渲染詳解
這篇文章主要介紹了React如何有效減少使用useContext導致的不必要渲染,使用useContext在改變一個數(shù)據(jù)時,是通過自己逐級查找對比改變的數(shù)據(jù)然后渲染,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11useEffect中return函數(shù)的作用和執(zhí)行時機解讀
這篇文章主要介紹了useEffect中return函數(shù)的作用和執(zhí)行時機,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01