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 Building Applications with React and Redux Modularizing the React Scoreboard Application Cleaning up the Scoreboard Container Component

Lucas Santos
Lucas Santos
19,315 Points

Why does he define state to a component without a constructor?

Iv'e never seen someone define states in React outside of a constructor? Why did he do this?

This is what I normally do and see:

import React, { Component } from 'react';

class Something extends Component{

    constructor(props){
        super(props);
        this.state = {
            name: "Bob"
        }
    }

    render(){
        return(
            <div className="something"></div>
        );
    }
}

export default Something;

1 Answer

Michael Liendo
Michael Liendo
15,326 Points

Hey Lucas,

He's using what's known as a property initializer. It's a feature of es7 classes. It's experimental, but I believe it's a stage-3 proposal, meaning most likely it'll be added in the next version. Feel free to google around and check it out. But all you really need to know is that it's a nice way of doing the same thing.