【问题标题】:TypeError: this.setState is not a function after 5 seconds [duplicate]TypeError:this.setState在5秒后不是一个函数[重复]
【发布时间】:2020-12-15 23:26:21
【问题描述】:

我试图在 5 秒后将值设置为状态。但 5 秒后,它在 this.setState({text:false}) 行显示错误。

代码 sn-p

 constructor(props){
    super(props)
    this.state={
        text:true,
        }
      }
 loadtext(){
 setTimeout(() => {
  this.setState({text:false});
  },5000)

错误:

TypeError: this.setState 不是函数

如何解决?

【问题讨论】:

  • 下次请research your error - 将标题更改为错误后,我什至无法保存您的问题,因为它已经存在

标签: javascript reactjs typeerror setstate


【解决方案1】:

你需要绑定loadtextfoo

 constructor(props){
    super(props);
    this.loadtext = this.loadtext.bind(this); // here change
    this.state={
        text:true,
    }
 }

 loadtext() {
    setTimeout(() => {
    this.setState({text:false});
    }, 5000)
 }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-11
  • 2015-06-22
  • 2011-11-03
  • 2023-04-09
  • 2018-04-27
  • 1970-01-01
相关资源
最近更新 更多