-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.

2
  • Never post code as image. Commented Feb 17, 2017 at 22:55
  • Thank you very much, it's work now Commented Feb 18, 2017 at 15:40

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

3 Comments

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.
That should work fine, yes. But you might already have a constructor anyway, for instance to bind methods to this or the like.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.