question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'state' of undefined with create-react-class

See original GitHub issue

I am currently attempting to create a button which is red with the text “Yes” that when you click on it the button changes to a green color with the text “Confirm?” before the final stage in which an action takes place. Where I am currently at is defining buttonColor as a state which changes on the click of the button; the initial color should be #FD8F83 and the final color after the click should be #A4D87C. However, I am currently getting the error “TypeError: Cannot read property ‘state’ of undefined” pointing to the style={{backgroundColor: this.state.buttonColor}} line whenever the code is compiled on the webpage.

Defining initial state and behavior on click:

getInitialState: function() {
    	return {
    		buttonColor: "#FD8F83"
    	};
},
handleClick(color) {
    	this.setState({
		buttonColor: color
	}); 
}

Code inside table in render():

<td>
	<button 
		className="removeButton" 
		style={{backgroundColor: this.state.buttonColor}} 
		onClick={function(){this.handleClick("#A4D87C")}}>
		Yes
	</button>
</td>

Does anyone have any ideas why this is? I am brand new to React so I apologize if it’s obvious. I also learned React using createClass so I’ve been trying to piece together how to make this work with the new create-react-class package. Any advice is greatly appreciated!

React: ^16.2.0 Chrome: Version 63.0.3239.132 (Official Build) (64-bit)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

62reactions
DCtheTallcommented, Jan 25, 2018

@dallashuggins replace

this.props.customers.map(function(customer, index) {

with

this.props.customers.map((customer, index) => {

and that should fix it. The function keyword switches the context of this to be whatever object the function is bound to, in this case it’s undefined. Arrow functions were added to JavaScript because of this behavior since they do not change the context of this.

Also for future reference this is an issue thats better for some place like Stack Overflow, these threads are for tracking bugs.

56reactions
gaearoncommented, Jan 25, 2018

The problem is:

onClick={function(){this.handleClick("#A4D87C")}}

In JS this does not get preserved this way.

If you want to keep this value from the render method you can use an arrow function:

onClick={() => {this.handleClick("#A4D87C")}}

Hope this helps!

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Cannot Read Property 'state' of undefined for this.state
I'm new to react and I was wonder why I keep getting this cannot read property value of "undefined ...
Read more >
React Without ES6
Setting the Initial State ... In ES6 classes, you can define the initial state by assigning this.state in the constructor: class Counter extends...
Read more >
Properties of the uninitialized this.props should not be accessed
(TypeError: Cannot read property 'x' of undefined) } render() ... {this.state.x}</div>); } } // Example 2 import createReactClass from ...
Read more >
How to use React createRef - LogRocket Blog
When working with refs in React, you may run into the problem — "TypeError: Cannot read properties of undefined" . This error can...
Read more >
TypeError: Cannot read property state of undefined - Michael S
3 ways not to see 'TypeError: Cannot read property 'state' of undefined' error. I wanted to make clear about it when I took...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found