Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.2k 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 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...