From 9dca9dbbea14f3fbe9b1c9cc2d9ff6b418f0797e Mon Sep 17 00:00:00 2001 From: chenxuan Date: Fri, 12 Apr 2024 09:58:34 +0800 Subject: [PATCH] no message --- basic/6/P05.html | 8 ++++++-- basic/6/P06.html | 15 ++++----------- basic/6/P07.html | 3 +-- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/basic/6/P05.html b/basic/6/P05.html index 77dd1f55d..8f981ccc5 100644 --- a/basic/6/P05.html +++ b/basic/6/P05.html @@ -38,11 +38,15 @@ this.forceUpdate() } // 定义在类自身,接收props state两个参数,必须返回状态对象 - // 返回状态对象后会劫持原有state state的值在任何时候都取决于props + // 返回状态对象后会劫持原有state + // 返回null则不劫持原有state + // 应用场景 state值与props相关 + static getDerivedStateFromProps(props,state){ console.log("xx") console.log(props,state) - return props + // return props + return null } render(){ console.log("3-render") diff --git a/basic/6/P06.html b/basic/6/P06.html index 65b705712..c67e25727 100644 --- a/basic/6/P06.html +++ b/basic/6/P06.html @@ -30,21 +30,12 @@ count+=1; this.setState({count}) } - death=()=>{ - ReactDOM.unmountComponentAtNode(document.getElementById('app')) - } - // 强制更新不受shouldComponentUpdate控制 - force=()=>{ - this.forceUpdate() - } render(){ console.log("3-render") return (

当前求和为:{this.state.count}

- -
) } @@ -60,8 +51,10 @@ return true } // 组件更新之前搞一些事情 - getSnapshotBeforeUpdate(){ - return "xxx" + // 次钩子函数的任何值都会传递给componentDidUpdate + getSnapshotBeforeUpdate(prevProps,prevState){ + // console.log(preState) + return "getSnapshotBeforeUpdate" } componentDidUpdate(preProps,preState,snapshotValue){ console.log(snapshotValue) diff --git a/basic/6/P07.html b/basic/6/P07.html index 08e902ea6..6682e613f 100644 --- a/basic/6/P07.html +++ b/basic/6/P07.html @@ -11,7 +11,7 @@ Document - getSnapshotBeforeUpdate + getSnapshotBeforeUpdate实践 @@ -50,7 +50,6 @@ this.refs.list.scrollTop+=this.refs.list.scrollHeight-height } render(){ - // console.log(this.state.news) return (
{