-2

I'm trying to set the value in the state after 5 secs. But after 5 secs it showing the error at this.setState({text:false}) line.

Code snippet

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

Error:

TypeError: this.setState is not a function

How to solve this?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Next time please [research your error](https://www.google.com/search?q=TypeError:+this.setState+is+not+a+function+site:stackoverflow.com) - I could not even save your question after changing the title to the error because it already existed – mplungjan Aug 27 '20 at 05:51

1 Answers1

0

You need to bind loadtext foo

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

 loadtext() {
    setTimeout(() => {
    this.setState({text:false});
    }, 5000)
 }
Ravi Garg
  • 1,378
  • 12
  • 23
Jay
  • 2,826
  • 2
  • 13
  • 28
  • [Huge dupe](https://www.google.com/search?q=TypeError:+this.setState+is+not+a+function+site:stackoverflow.com) – mplungjan Aug 27 '20 at 05:51