-1

please help me in the code. i have a problem with getInitialState(), the code isn't work: enter image description here

the mistake is on the picture, but i can't see this.

John Smith
  • 69
  • 11

1 Answers1

4

You're using ES6 classes, which means you need to use a constructor to initialize the state:

class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            show: true
        };
    }

    // ..
}

Reference: What is the difference between using constructor vs getInitialState in React / React Native?

Community
  • 1
  • 1
  • Bingo. Question, though: is there any reason not to just add state={show:true} to the class itself, rather than introducing the constructor? I've done it that way and it seems to work fine. – Joshua Engel Feb 17 '17 at 22:56
  • 1
    That should work fine, yes. But you might already have a constructor anyway, for instance to bind methods to `this` or the like. –  Feb 17 '17 at 23:02
  • You can either initialize the state within the constructor, as shown in the answer, or just using the static property 'state' within the class itself, as mentioned in the previous comment. – nbkhope Feb 17 '17 at 23:49