Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript React Basics (2018) Understanding State Create a Stateful Component

IDE does not recognize this.state within constructor

My IDE is not recognizing this.state = {}. I receive an Unused definition state validation message and when I run the code on my local server, I don't see a State attribute under the React window of the code inspector.

class Counter extends React.Component {

  constructor() {
    super()
    this.state = {
      score: 0
    };
  }

  render() {
    return (
      <div className="counter">
        <button className="counter-action decrement"> - </button>
        <span className="counter-score">{ this.state.score }</span>
        <button className="counter-action increment"> + </button>
      </div>
    );
  }
}

This is working correctly within Workspaces, so I've been able to continue, but would like to know if there are any additional updates needed in order to use an IDE and local server.

Thanks.

is your code running or getting an error message ? What IDE are you using ?

Fernando Boza -- My code runs, but it is treating this.state as a declared method, rather than part of the React constructor.

I have been using IntelliJ 2018.1.5 Ultimate Edition

1 Answer

Got this one figured out. I did not correctly create a react app through the npx create-react-app. I jumped the shark a bit with the React track, but everything is working now.

Jakub Drobina
Jakub Drobina
12,675 Points

Can u explain how you did it? i got the same problem and i cant solve it i created new react app by typing in cmd :

npx create-react-app my aplication

and then

npm start

and nothing changed. I cant see state i my console

Can someone help me Please? Im stuck

Thank you