javascript React-未捕获类型错误:无法读取未定义[重复]的属性"setState"

dldeef67  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(95)
    • 此问题在此处已有答案**:

React - uncaught TypeError: Cannot read property 'setState' of undefined(20个答案)
20小时前关闭。

`I am getting the following error

React-未捕获类型错误:无法读取未定义的属性'setState'

import React, { Component } from "react";

 class Message extends Component {
   constructor() {
     super();
     this.state = {
       message: "Welcome Visitor",
     };
   }

  changeMessage(event) {
    console.log("i am here");
    this.setState({
      message: "thank you for subscribing",
    });
  }

  render() {
    return (
      <div>
        <h1>{this.state.message}</h1>
        <button onClick={this.changeMessage}>subscribing</button>
      </div>
    );
  }
}

export default Message;

我已经创建了以下代码,但我不断地得到以下错误(很多)。它看起来像应用程序进入了一个循环:

bd1hkmkf

bd1hkmkf1#

必须导入useState

import React, { Component, useState } from "react";

相关问题