Setting Initial State

A React component can access dynamic information in two ways: props and state.

Unlike props, a component's state is not passed in from the outside. A component decides its own state.

To make a component have state, give the component a state property. This property should be declared inside of a constructor method, like this:

class Example extends React.Component { constructor(props) { super(props); this.state = { mood: 'decent' }; } render() { return <div></div>; } } <Example />

Whoa, a constructor method? super(props)? What's going on here? Let's look more closely at this potentially unfamiliar code:

constructor(props) { super(props); this.state = { mood: 'decent' }; }

this.state should be equal to an object, like in the example above. This object represents the initial "state" of any component instance. We'll explain that more soon!

How about the rest of the code? constructor and super are both features of ES6, not unique to React. There is nothing particularly React-y about their usage here. A full explanation of their functionality is outside of the scope of this course, but we'd recommend familiarizing yourself. It is important to note that React components always have to call super in their constructors to be set up properly.

Look at the bottom of the highest code example in this column. <Example /> has a state, and its state is equal to { mood: 'decent' }.

Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.