【发布时间】:2021-11-10 16:25:29
【问题描述】:
当我更改状态时,我可以在主组件中读取 newData,但我的更新状态不会进入我的 ChildComponent。我在 ChildComponent 中只读取了 initialData。为什么我的新道具不去 ChildComponent?或者为什么不重新渲染我的 ChildComponent ?
class Main extends Component{
constructor(props) {
super(props);
this.state = {
data: "initalData",
}
}
componentDidMount(){
this.changeState();
}
changeState(){
this.setState({data: "newData"})
}
render(){
console.log("my data in Main :",this.state.data)
return(
<ChildComponent dataProps={this.state.data}>
)
}
class ChildComponent extends Component{
constructor(props) {
super(props);
const getData = props.dataProps;
console.log("getData is ",getData)
this.state = {
...,
}
}
}
【问题讨论】:
标签: reactjs react-props react-component