Initialization & Construction

During the initialization of the Component from the Element, the props and state are defined. How these values are defined depends on if you are using React.createClass() or extend React.Component. Let’s first look at props and then we will examine state.

Default Props

As we mentioned earlier, the Element instance contains the current props that are being passed to the Component instance. Most of the time, all the available props on the Component are not required. Yet, some times we do need to have values for all the props for our Component to render correctly.

For example, we have a simple component that renders a name and age.

  1. import React from 'react';
  2. export default class Person extends React.Component {
  3. render() {
  4. return (
  5. <div>{ this.props.name } (age: { this.props.age })</div>
  6. );
  7. }
  8. }

In our case, we expect two props to be passed in: name and age. If we want to make age optional and default to the text ‘unknown’ we can take advantage of React’s default props.

For ES6 Class

  1. import React from 'react';
  2. class Person extends React.Component {
  3. render() {
  4. return (
  5. <div>{ this.props.name } (age: { this.props.age })</div>
  6. );
  7. }
  8. }
  9. Person.defaultProps = { age: 'unknown' };
  10. export default Person;

For createClass (ES6/ES5/CoffeeScript, etc.)

  1. var Person = React.createClass({
  2. getDefaultProps: function() {
  3. return ({ age: 'unknown' });
  4. },
  5. render: function() {
  6. return (
  7. <div>{ this.props.name } (age: { this.props.age })</div>
  8. );
  9. }
  10. });

The result of either process is the same. If we create a new instance without setting the age prop ex: <Person name="Bill" />, the component will render <div>Bill (age: unknown)</div>.

React handles default props by merging the passed props object and the default props object. This process is similar to Object.assign() or the Lodash/Underscore _.assign() method. The default props object is the target object and the passed props is the source:

  1. // React library code to extract defaultProps to the Constructor
  2. if (Constructor.getDefaultProps) {
  3. Constructor.defaultProps = Constructor.getDefaultProps();
  4. }
  5. // psuedo code (as an example)
  6. this.props = Object.assign(Constructor.defaultProps, elementInstance.props);

In the React code snippet, React checks the underlying Class instance to see if it defines getDefaultProps() and uses this to set the values. When using ES6 classes we just define it on the class itself. Any property defined on the passedProps value is applied/overridden to the property in the default props object.

null vs. undefined props

When using default props, it is important to understand how the React merge process works. Often, we are generating props dynamically based on application state (Flux, Redux, Mobx, etc.). This means that we can sometimes generate null values and pass this as the prop.

When assigning default props, the React object merge code sees null as a defined value.

  1. <Person name="Bob" age={ null } />

Because null is a defined value our Component would render this as <div>Bob (age:)</div> instead of rendering unknown. But, if we pass in undefined instead of null, React treats this as undefined (well yeah, obviously) and we would render unknown as expected.

Keep this in mind when defining default props, because tracing down a null value can be tricky in larger application.

Initial State

Once the final props are defined (passed w/ defaults), the Component instance configures the initial state. This process occurs in the construction of the instance itself. Unlike props, the Component state is an internal object that is not defined by outside values.

To define the initial state depends on how you declare your Component. For ES6 we declare the state in the constructor. Just like defaultProps, the initial state takes an object.

For ES6 Class

  1. import React from 'react';
  2. class Person extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = { count: 0 };
  6. }
  7. render() {
  8. return (
  9. <div>{ this.props.name } (age: { this.props.age })</div>
  10. );
  11. }
  12. }
  13. Person.defaultProps = { age: 'unknown' };
  14. export default Person;

For React.createClass Components, there is a helper method called getInitialState() which returns the state object. This method is called during setup to set the state on the instance.

For createClass (ES6/ES5/CoffeeScript, etc.)

  1. var Person = React.createClass({
  2. getDefaultProps: function() {
  3. return ({ age: 'unknown' });
  4. },
  5. getInitialState: function() {
  6. return ({ count: 0 });
  7. },
  8. render: function() {
  9. return (
  10. <div>{ this.props.name } (age: { this.props.age })</div>
  11. );
  12. }
  13. });

State defaults

It is important to keep in mind that if we do not define a state in the constructor/getInitialState then the state will be undefined. Because the state is undefined and not an empty Object ({}), if you try to query the state later on this will be an issue.

In general, we want to set a default value for all our state properties. There are some edge cases where the initial value for the state property may be null or undefined. If this state happens to be only state property, it may be tempting to skip setting a default state. But, if our code tries to access the property you will get an error.

  1. class Person extends React.Component {
  2. render() {
  3. // This statement will throw an error
  4. console.log(this.state.foo);
  5. return (
  6. <div>{ this.props.name } (age: { this.props.age })</div>
  7. );
  8. }
  9. }

The log statement fails because this.state is undefined. When we try to access foo we will get a TypeError: Cannot read property ‘foo’ of null. To solve this we can either set the default state to {} or, to have a clearer intention, set it to { foo: null }.

Next Up: Pre-mounting with componentWillMount()