1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| import { Component } from "react";
export default class Example extends Component { state = { name: "来自父组件" } hanldClick = ()=>{ this.setState({ name:"我是被父组件修改的数据" }) } render() { return ( <> <div>我是父组件</div> <button onClick={this.hanldClick}>我要修改子组件的数据</button> <br></br> <B name={this.state.name}></B> </> ); } }
class B extends Component { componentWillReceiveProps(){ console.log("componentWillReceiveProps ==> 被执行") } shouldComponentUpdate(){ console.log("shouldComponentUpdate ==> 被执行") return true } componentWillUpdate(){ console.log("componentWillUpdate ==> 不改变状态强制更新") } hanldClick = () =>{ this.forceUpdate() } render() { return <> <h2>我是子组件下面的数据</h2> <div>{this.props.name}</div> <button onClick={this.hanldClick}>强制更新</button> </>; } }
|