错误消息: 类型错误: 在React内部无法读取属性 ‘count’ 的值

huangapple go评论70阅读模式
英文:

Error Message : Type Error: Cannot read property 'count' of null Inside React

问题

获取到此错误:
错误信息:TypeError: 无法读取 null 的属性 'count'

英文:

class MyComponent extends React.Component {
  constructor() {
    super();
    this.state = {
      count: 0,
    };
  }

  handleClick() {
    this.setState({ count: this.state.count + 1 });
  }

  render() {
    return (
      <div>
        <h1>Counter: {this.state.count}</h1>
        <button onClick={this.handleClick}>Increment</button>
      </div>
    );
  }
}

export default MyComponent;

getting this error:
Error Message: TypeError: Cannot read property 'count' of null

anyone who can help me, What is the error in my code.

答案1

得分: 1

当你从<button />onClick事件中调用handleClick时,它失去了对组件实例的引用,导致this关键字指向null而不是组件实例。

通过将this.handleClick绑定到组件实例,确保handleClick内部的this关键字引用组件实例,从而允许你正确访问this.state.count(或更好地说是this.state)。

你也可以使用箭头函数。

箭头函数会自动“捕获”组件实例中的this值。因此,你可以在handleClick函数内访问this.state.count(或更好地说是this.state)。

英文:

When you call handleClick from the <button />'s onClick event, it loses its reference to the component's instance, resulting in the this keyword pointing to null instead of the component instance.

class MyComponent extends React.Component {
  constructor() {
    super();
    this.state = {
      count: 0,
    };

    // Bind the handleClick method to the component instance
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    this.setState({ count: this.state.count + 1 });
  }

  render() {
    return (
      <div>
        <h1>Counter: {this.state.count}</h1>
        <button onClick={this.handleClick}>Increment</button>
      </div>
    );
  }
}

export default MyComponent;

By binding this.handleClick to the component instance, you make sure that the this keyword inside the handleClick refers to the component instance, allowing you to access this.state.count (or better this.state) properly.

You also could use an arrow function.

class MyComponent extends React.Component {
  constructor() {
    super();
    this.state = {
      count: 0,
    };
  }

  handleClick = () => {
    this.setState({ count: this.state.count + 1 });
  };

  render() {
    return (
      <div>
        <h1>Counter: {this.state.count}</h1>
        <button onClick={this.handleClick}>Increment</button>
      </div>
    );
  }
}

export default MyComponent;

The arrow function automatically "captures" the this value from the component instance. Because of that, you can access this.state.count (or better this.state) within the handleClick function.

答案2

得分: 1

需要绑定这个函数/方法:

render() {
  return (
    <div>
      <h1>计数器{this.state.count}</h1>
      <button onClick={this.handleClick.bind(this)}>增加</button>
    </div>
  );
}
英文:

Need to bind the function/method

render() {
        return (
          &lt;div&gt;
            &lt;h1&gt;Counter: {this.state.count}&lt;/h1&gt;
            &lt;button onClick={this.handleClick.bind(this)}&gt;Increment&lt;/button&gt;
          &lt;/div&gt;
        );
      }

答案3

得分: -1

使用箭头函数来定义handleClick方法:

handleClick = () => {
  this.setState({ count: this.state.count + 1 });
}

在构造函数中显式绑定this上下文:

constructor() {
  super();
  this.state = {
    count: 0,
  };
  this.handleClick = this.handleClick.bind(this);
}
英文:

> Use an arrow function for the handleClick method:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

handleClick = () =&gt; {
  this.setState({ count: this.state.count + 1 });
}

> Bind the this context explicitly in the constructor:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

constructor() {
  super();
  this.state = {
    count: 0,
  };
  this.handleClick = this.handleClick.bind(this);
}

huangapple
  • 本文由 发表于 2023年6月19日 14:59:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定