Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

最近在学习React 中遇到了点问题,我的数据改变了,但是视图就是不发生变化~~
如图:
image.png

// 导入包
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"));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

复现不了
https://codesandbox.io/s/purp...
我这边无论是点按钮还是使用react开发插件直接修改state都能正常触发视图变化
看了下代码应该也是没问题的


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...