最近在学习React 中遇到了点问题,我的数据改变了,但是视图就是不发生变化~~
如图:
// 导入包
import React from "react";
import ReactDOM from "react-dom";
// 创建组件
class Father extends React.Component {
state = {
list: 0,
};
haddle = () => {
this.setState({
list: this.state.list + 1,
});
};
render() {
return (
<div>
<h1>点击按钮后我+1:{this.state.list}</h1>
<Child mag={this.haddle} />
</div>
);
}
}
// 创建子组件
class Child extends React.Component {
render() {
return (
<div>
<button onClick={this.ClickBtn}>+1</button>
</div>
);
}
ClickBtn = () => {
this.props.mag();
};
}
// 渲染
ReactDOM.render(<Father />, document.getElementById("root"));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…