Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

React 数据变化,视图层不发生变化??求大佬看看~~~

最近在学习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"));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...