H
H
heIIfire2017-09-25 21:21:34
React
heIIfire, 2017-09-25 21:21:34

Where to initialize the state of a React component?

So, what's the difference between these options?

class C extends ReactComponent{
    constructor(props){
        super(props);
        this.state = {
            isAnimal: true
        }
    }
    render(......)
}

class C extends ReactComponent{
    state = {
        isAnimal: true
    }
    render(........)
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RubaXa, 2017-09-25
@RubaXa

In the first one, you have access to `props` and `context` (constructor arguments).
The second, yet unstandardized "sugar" to the first, but without `props` and `context`.

M
Mikhail Osher, 2017-09-25
@miraage

The second option won't work without babel-plugin-transform-class-properties .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question