Error Unable to access React instance this inside event handler

0 votes

I am writing a simple component in ES6 (with BabelJS), and functions this.setState is not working.

Typical errors include something like

Cannot read property 'setState' of undefined

or

this.setState is not a function

Here is the code:

import React from 'react'

class SomeClass extends React.Component {
  constructor(props) {
    super(props)
    this.state = {inputContent: 'startValue'}
  }

  sendContent(e) {
    console.log('sending input content '+React.findDOMNode(React.refs.someref).value)
  }

  changeContent(e) {
    this.setState({inputContent: e.target.value})
  } 

  render() {
    return (
      <div>
        <h4>The input form is here:</h4>
        Title: 
        <input type="text" ref="someref" value={this.inputContent} 
          onChange={this.changeContent} /> 
        <button onClick={this.sendContent}>Submit</button>
      </div>
    )
  }
}
export default SomeClass

How to solve this error?

Jun 4, 2020 in Angular by kartik
• 37,520 points
1,948 views

1 answer to this question.

0 votes

Hello @kartik,

You can use an arrow function together with the class properties proposal:

class SomeClass extends React.Component {
  changeContent = (e) => {
    this.setState({inputContent: e.target.value})
  } 

  render() {
    return <input type="text" onChange={this.changeContent} />;
  }
}

Because the arrow function is declared in the scope of the constructor, and because arrow functions maintain this from their declaring scope, it all works. The downside here is that these wont be functions on the prototype, they will all be recreated with each component. However, this isn't much of a downside since bind results in the same thing.

Hope this works!!

If you need to know more about React, Its recommended to join React JS Course today.

Also Read: Error: EACCES: permission denied, access '/usr/local/lib/node_modules

Thank You!!

answered Jun 4, 2020 by Niroj
• 82,840 points

Related Questions In Angular

0 votes
1 answer

How can we go back to previous page after having some error on request made through current page ?

$route is used for deep-linking URLs to controllers ...READ MORE

answered Feb 11, 2020 in Angular by Niroj
• 82,840 points
1,253 views
0 votes
1 answer

How to allow access outside localhost?

Hello kartik, You can use the following command ...READ MORE

answered Apr 22, 2020 in Angular by Niroj
• 82,840 points
8,720 views
0 votes
1 answer

How to have conditional elements and keep DRY with Facebook React's JSX?

Hello @kartik, Let's define a simple helping If component: var If ...READ MORE

answered Jun 4, 2020 in Angular by Niroj
• 82,840 points
693 views
+1 vote
1 answer

How can i create simple register form using html and css?

Html5 contains lots of elements using which ...READ MORE

answered Jan 31, 2020 in HTML by Niroj
• 82,840 points
4,840 views
0 votes
1 answer

Explain general Routing workflow between several web pages?

hii, Routing is just another way of fixing some content ...READ MORE

answered Feb 10, 2020 in Angular by Niroj
• 82,840 points
732 views
0 votes
1 answer

How can we Create Multiple Where Clause Query Using Laravel Eloquent?

Hii, You can use Conditions using Array: $users = User::where([ ...READ MORE

answered Mar 30, 2020 in Laravel by Niroj
• 82,840 points
16,743 views
0 votes
1 answer

How to loop inside React JSX?

Hello @kartik, You're just calling JavaScript functions. You ...READ MORE

answered Jul 22, 2020 in Angular by Niroj
• 82,840 points
1,721 views
0 votes
2 answers

Error: EACCES: permission denied, access '/usr/local/lib/node_modules' react

Hello @kartik, Change your file permissions like this First ...READ MORE

answered Jul 22, 2020 in Angular by Niroj
• 82,840 points
23,099 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP