Over a million developers have joined DZone.
Refcard #224

React.js Essentials

The Simple Front-end Library for User Interfaces

Written by

Hemanth HM FullStack Developer, FOSS

Gives you the essentials of React.js, from the architecture to the virtual DOM, from building components to styling them.

Free PDF
DOWNLOAD
Section 1

What Is ReactJS?

React (a.k.a. ReactJS or React.js) is a JavaScript library for creating user interfaces, open sourced to the world by Facebook and Instagram team in 2013. One might think of it as the “View” in the “Model-View-Controller” pattern.

React’s main goal is to be simple, declarative, and compassable, as it is intended to ease the process of building large applications using data that changes over time.

React was created by Jordan Walke, a software engineer at Facebook, with the influence of XHP, a PHP-based component system that is still in use at Facebook, but also by functional programming ideas. Pete Hunt wanted to use React at Instagram, so he pushed to extract React from Facebook-specific code and open source it. (Editor's note: Some have expressed concern about the React license; see discussions here, here, and most recently (Jan2016) here, with discussion of some alternatives here. We're not lawyers but figure it's important to be aware of this sort of thing.)

React has lately gained a lot of popularity for its concept of a “virtual-DOM,” which allows it to determine which parts of the DOM have changed by diffing the new version with the stored virtual DOM, and using the result to determine how to most efficiently update the browser's DOM.

Section 2

How the Magic Happens

To get an idea of what’s going on inside React, take a look at the following diagrams demonstrating how React.js rendering and the React Virtual DOM work:

Image title

Figure 1: React.js Rendering

Image title

Figure 2: The React Virtual DOM

React basically batches DOM updates and then applies minimal diffs to the real DOM.

Section 3

A Simple "Hello" Component

React components implement a render() method that takes input data and returns what to display.

Here is a simple example of a "Hello" Component:

var Hello = React.createClass({
  render: function () {
    return <div>Hello {this.props.name}</div>;
  }
});

React.render(<Hello name="World" />, document.body);

Note: The XML-like syntax is called JSX.

Here's the same component without JSX:

var Hello = React.createClass({displayName: "Hello",
  render: function() {
    return React.createElement("div", null, "Hello ", this.props.name);
  }
});

React.render(React.createElement(Hello, {name: "World"}), mountNode);
Section 4

Component Specifications:

render

The render() function should be pure, meaning that it does not modify the component state. It should examine this.props and this.state and return a single child element.

getInitialState

Invoked once before the component is mounted. The return value will be used as the initial value of this.state.

getDefaultProps

Invoked once and cached when the class is created. Values in the mapping will be set on this.props.

propTypes

The propTypes object allows you to validate props being passed to your components.

mixins

The mixins array allows you to use mixins to share behavior among multiple components.

statics

The statics object allows you to define static methods that can be called on the component class.

displayName

The displayName string is used in debugging messages. JSX sets this value automatically.

Components API

setState

Merges nextState with the current state.

replaceState

Like setState(), but deletes any pre-existing state keys that are not in nextState.

forceUpdate

Call render() on the component, skipping shouldComponentUpdate()

 React.findDOMNode (0.13+)

Returns the corresponding native browser DOM element.

isMounted

isMounted() returns true if the component is rendered into the DOM.

setProps

To change the properties and trigger re-render

replaceProps

Like setProps() but deletes any pre-existing props instead of merging the two objects.

Lifecycle Methods

componentWillMount

Invoked once, both on the client and server, immediately before the initial rendering occurs.

componentDidMount

Invoked on the client immediately after the initial rendering occurs.

componentWillReceiveProps

Invoked when a component is receiving new prop. Use setState() here.

shouldComponentUpdate

Invoked before rendering when new props or state are being received. Skips render() if returns false.

componentWillUpdate

Invoked immediately before rendering when new props or state are being received. Can’t use setState() here.

componentDidUpdate

Invoked immediately after the component's updates are flushed to the DOM. Operate on the DOM here.

componentWillUnmount

Invoked immediately before a component is unmounted from the DOM.

States and Properties

Props and states are both plain JS objects; a change within one of them will trigger a render. These objects are deterministic.

<VideoComponent fullscreen={true} />
// props
  this.props.fullscreen //=> true
// state
  this.setState({ user: 'hemanth' });
  this.replaceState({ ... });
  this.state.username //=> 'hemanth'
  render: function () {
    return <div className={this.props.fullscreen ? 'full' : ''}>
      Hello, {this.state.username}
    </div>;
}

Pre-populates states and props:

React.createClass({
  getInitialState: function () {
    return { comments: [] };
  },
  getDefaultProps: function () {
    return { name: "Hello" };
  }
);

Deciding when to use props and went to use state might get tricky. The following table will help to simplify this decision:

Deciding factor

props

state

Can get initial value from parent Component?

Yes

Yes

Can be changed by parent Component?

Yes

No

Can set default values inside Component?

Yes

Yes

Can change inside Component?

No

Yes

Can set initial value for child Components?

Yes

Yes

Can change in child Components?

Yes

No


Section 5

AJAX Requests

React by default doesn’t provide a helper method to manage AJAX requests, but you can use any other third party JavaScript library—like jQuery or Zepto—to make necessary AJAX requests.

Below is a sample code snippet that performs an AJAX request on props.url and on success sets the data state. In case of  an error, it just uses console.error to report the error.

Note: Make sure that the execution context (this) is bound to the success and error callbacks.

componentDidMount: function() {
    $.ajax({
      url: this.props.url,
      dataType: 'json',
      cache: false,
      success: function(data) {
        this.setState({data: data});
      }.bind(this),
      error: function(xhr, status, err) {
        console.error(this.props.url, status, err.toString());
      }.bind(this)
    });
  }
Section 6

Styling Your Components

In React, styles are mentioned in line, but unlike the traditional way of inline CSS strings, here we specify each style as an object whose key is the camelCased version of the style name, and whose value is the style's value (usually a string).

var divStyle = {
  color: 'white',
  backgroundImage: 'url(' + imgUrl + ')',
  WebkitTransition: 'all', // note the capital 'W' here
  msTransition: 'all' // 'ms' is the only lowercase vendor prefix
};
React.render(<div style={divStyle}>Hello World!</div>, mountNode);

Most numeric values at the end of a style prop receive an automatic “px” specification added to them (e.g., “width: 10” is read as “width: 10px”). Here is a list of properties that won't get the automatic "px" suffix:

  • boxFlex

  • boxFlexGroup

  • columnCount

  • fillOpacity

  • flex

  • flexGrow

  • flexPositive

  • flexShrink

  • flexNegative

  • fontWeight

  • lineClamp

  • lineHeight

  • opacity

  • order

  • orphans

  • strokeOpacity

  • widows

  • zIndex

  • zoom

Section 7

DOM Helpers

References

Helps to access the DOM nodes.

<input ref="firstName">
this.refs.firstName
React.findDOMNode(this.refs.firstName).focus()
React.findDOMNode(this.refs.firstName).value
DOM Events: Helps to handle DOM events.
<input type="text" value={this.state.value} onChange={this.handleChange} />
handleChange: function(event) {
  this.setState({ value: event.target.value });
}

Two-way data bindings with mixins

Email: <input type="text" valueLink={this.linkState('email')} />
React.createClass({
  mixins: [React.addons.LinkedStateMixin]
});
this.state.email

Validating properties

Primitive types: .string, .number, .func, and .bool.

React elements: .element, .node.

Enumerables: .oneOf, .oneOfType.

Arrays and objects: array[Of], .object[Of], .instanceOf, .shape.

Sample usage:

React.createClass({
  propTypes: {email: React.PropTypes.string, firstName: React.PropTypes.string, age:  React.PropTypes.number, gender: React.PropTypes.oneOf(['M','F','NA']) 
    node: React.PropTypes.node, cb: React.PropTypes.func.isRequired,
  }
});

Custom Validation

propTypes: {
  customProp: function(props, propName, componentName) {if (!/matchme/.test(props[propName])) {return new Error('Validation failed!');}
  }
}

React Addons

React.addons are useful utilities for building React apps, these are currently experimental, and not yet part of Core React.

  • TransitionGroup and CSSTransitionGroup, for dealing with animations and transitions

  • LinkedStateMixin helps in two way data binding.

  • cloneWithProps, to make shallow copies of React components and change their props.

  • createFragment, helps to create a set of externally-keyed children.

  • update, helps to deal with immutable data.

  • PureRenderMixin, a performance booster under certain situations.

Apart from these there are few which are available in the development (unminified) version of React only:

  • TestUtils, simple helpers for writing test cases

  • Perf, for measuring performance and giving you hint where to optimize.

To get the add-ons, use react-with-addons.js (and its minified counterpart) rather than the common react.js.

When using the react package from npm, simply require('react/addons') instead of require('react') to get React with all of the add-ons.

  • NOTE: Add-ons have moved to separate packages in React v0.14+:
    • react-addons-clone-with-props
    • react-addons-create-fragment
    • react-addons-css-transition-group
    • react-addons-linked-state-mixin
    • react-addons-perf
    • react-addons-pure-render-mixin
    • react-addons-shallow-compare
    • react-addons-test-utils
    • react-addons-transition-group
    • react-addons-update
    • ReactDOM.unstable_batchedUpdates in react-dom
  • Creating Your Own Mixins

    var TimeOutMixin = {
      componentWillMount: function() { .. }
    }
    var TickTock = React.createClass({
      mixins: [TimeOutMixin]
    }

    React on ES2015/ES6:

    These are experimental and you must use a transpiler for this to work

    Classes

    class Animal extends React.Component {
      render() {
          return <img alt={this.props.name} src={this.props.src} />;
      }
    }

    Property Initializers

    var Video = React.createClass({
      getDefaultProps: function() {
                                        return {
                                        autoPlay: false,
                                        maxLoops: 10,
                                        };
      },
      getInitialState: function() {
                                        return {
                                        loopsRemaining: this.props.maxLoops,
                                        };
      },
      propTypes: {
                                        autoPlay: React.PropTypes.bool.isRequired,
                                        maxLoops: React.PropTypes.number.isRequired,
                                        posterFrameSrc: React.PropTypes.string.isRequired,
                                        videoSrc: React.PropTypes.string.isRequired,
      },
    });

    Arrow Functions

    class PostInfo extends React.Component {
      handleOptionsButtonClick = (e) => {
                                        this.setState({showOptionsModal: true});
      }
    }

    Dynamic property names with template strings

    class Form extends React.Component {
      onChange(inputName, e) {
                                        this.setState({
                                        [`${inputName}Value`]: e.target.value,
                                        });
      }
    }

    Destructuring & spread attribute

    class AutoloadingPostsGrid extends React.Component {
      render() {
                                        var {
                                        className,
                                        ...others,  // all properties of this.props except for className
                                        } = this.props;
                                        return <PostsGrid {...others} />
      }
    }

    CoffeeScript and React [React v0.13.0+]

    div = React.createFactory 'div'
    class Counter extends React.Component
      @propTypes = initialCount: React.PropTypes.number
      @defaultProps = initialCount: 0
      constructor: (props) ->
                                        super props
                                        @state = count: props.initialCount
      tick: =>
                                        @setState count: @state.count + 1
      render: ->
                                        div onClick: @tick,
                                        'Clicks: '
                                        @state.count
    Section 8

    Conventions for React DOM & JSX

    • React DOM elements are expected to be in camelCased.

    • the camelCasing of DOM matches what you would write for custom components: <Typeahead onClick=../> and <div onClick=.. />

    • These camelCased attributes are usually what you write when updating the DOM via JS (input.maxLength).

    • One current confusing exception is that class= is normalized automatically into className= at transform time.

    • Use className attribute instead of class

    • Use htmlFor attribute instead of for

    • Use <textarea value="something"> instead of <textarea>something</textarea>

    • Custom HTML attributes may be data-attr and all lower case.

    • Make use of ternary operators, wherever  required for example: React.render(<div id={condition ? 'msg' : ''}>Hello World!</div>, mountNode);

    Anti-pattern

    Props in getInitialState Is an Anti-Pattern:

    Instead of:

    var MessageBox = React.createClass({
      getInitialState: function() {
        return {nameWithQualifier: 'Mr. ' + this.props.name};
      };
      render: function() {
        return <div>{this.state.nameWithQualifier}</div>;
      }
    });
    React.render(<MessageBox name="Rogers"/>, mountNode);

    Try this:

    var MessageBox = React.createClass({
      render: function() {
        return <div>{'Mr. ' + this.props.name}</div>;
      }
    });
    React.render(<MessageBox name="Rogers"/>, mountNode);

    Note: It won’t be an anti-pattern if we make it clear that synchronization's not the goal.

    Spread Operator in JSX:

      var props = {};
      props.foo = x;
      props.bar = y;
      var component = <Component {...props} />;

    You can also use the spread operator (...) to override props:

      var props = { foo: 'default' };
      var component = <Component {...props} foo={'override'} />;
      console.log(component.props.foo); // 'override'

    The spread operator is already supported for arrays in ES6. There is also an ES7 proposal for Object Rest and Spread Properties.

    Section 9

    The Flux Application Architecture

    Flux plays a key role if your application use dynamic data.

    Don't try to compare Flux to Model View Controller (MVC) architecture, Flux is just a term to describe smart uni -directional data flow.


    Image title

    Figure 3: An Overview of Flux Architecture

    Keys ideas:

    • Views "Dispatch" "Actions".

    • "Store" Responds to Dispatched Events.

    •  Store Emits a "Change" Event.

    • View Responds to the "Change" Event

    Points to remember:

    • A "dispatcher" is essentially an event system.

    • There is at most one global dispatcher.

    • “Store” is specific collection of logic and data.

    • A “Store” is a singleton.

    • A store is not a model. A store containsmodels.

    •  A store is the only entity in the application that is aware of how to update data.

    • Only stores registers to dispatcher callbacks. store emits an event, but not using the dispatcher!

    • When store data changes, your views shouldn't care if things were added, deleted, or modified but just re-render.

    Section 10

    React v0.14

    React version 0.14 separated out all DOM related functionality into a separate dependency, which can be fetched from npm under the name `react-dom`

    React version 0.14 separated out all DOM related functionality into a separate dependency, which can be fetched from npm under the name `react-dom`

    Every new change in 0.14, including the major changes, is introduced with a runtime warning and will work as before until 0.15.

    The react package contains React.createElement, .createClass, .Component, .PropTypes, .Children, and the other helpers related to elements and component classes

    The react-dom package has ReactDOM.render, .unmountComponentAtNode, and .findDOMNode. In react-dom/server there is a server-side rendering support with ReactDOMServer.renderToString and .renderToStaticMarkup.

    So, if you wanted to duplicate the example Hello component from earlier with react v0.14, it would look like this:

    var React = require('react');
    var ReactDOM = require('react-dom');
    var Hello = React.createClass({
    render: function() {
    return <div>Hello World</div>;
    }
    });
    
    ReactDOM.render(<Hello/>, node);
    Section 11

    Testing Your Application With Jest

    Jest allows for painless JavaScript Unit Testing. It is built on top of the Jasmine test framework.

    Consider a scenario where you want to test the following div.js file:

    // sum.js
    function div (value1, value2) {
          return value1 /  value2;
                     }
                    module.exports = sum;
  • 1.    Create a directory __tests__/ with a file div-test.js
  • // __tests__/sum-test.js
    jest.dontMock('../sum');
    describe('sum', function() {
        it('divides 4 / 2 to equal 2', function() {
            var sum = require('../div);
            expect(div(4, 2)).toBe(2);
         });
    });

    2.    Run npm install jest-cli --save-dev

    3.    Add the following to your package.json

    { ... "scripts": { "test": "jest" } ... }

    4.    Run npm test

    [PASS] __tests__/sum-test.js (0.015s)

    Just follow the above steps, and you are ready to run with Jest!

    Note: For a react component, we follow the same steps along with it, we will have to use ‘React.addons.TestUtils`.

    Publications

    • Featured
    • Latest
    • Popular
    DOWNLOAD
    Design Patterns
    Learn design patterns quickly with Jason McDonald's outstanding tutorial on the original 23 Gang of Four design patterns, including class diagrams, explanations, usage info, and real world examples.
    198.9k 535.8k
    DOWNLOAD
    Core Java
    Gives you an overview of key aspects of the Java language and references on the core library, commonly used tools, and new Java 8 features.
    122.2k 321.6k
    DOWNLOAD
    Getting Started with Ajax
    Introduces Ajax, a group interrelated techniques used in client-side web development for creating asynchronous web applications.
    100.4k 196.6k
    DOWNLOAD
    Getting Started with Git
    This updated Refcard explains why so many developers are migrating to this exciting platform. Learn about creating a new Git repository, cloning existing projects, the remote workflow, and more to pave the way for limitless content version control.
    108.7k 241.8k
    DOWNLOAD
    Spring Configuration
    Catalogs the XML elements available as of Spring 2.5 and highlights those most commonly used: a handy resource for Spring context configuration.
    101.7k 254k
    DOWNLOAD
    Core CSS: Part I
    Covers Core principles of CSS that will expand and strengthen your professional ability to work with CSS. Part one of three.
    88.7k 191.5k
    DOWNLOAD
    jQuery Selectors
    Introduces jQuery Selectors, which allow you to select and manipulate HTML elements as a group or as a single element in jQuery.
    92k 347.8k
    DOWNLOAD
    Foundations of RESTful Architecture
    Introduces the REST architectural style, a worldview that can elicit desirable properties from the systems we deploy.
    90.6k 132.9k
    DOWNLOAD
    The Ultimate Scrum Reference Card
    Provides a concise overview of roles, meetings, rules, and artifacts within a Scrum organization. Updated for 2016.
    84.5k 222.5k
    DOWNLOAD
    Core Java Concurrency
    Helps Java developers working with multi-threaded programs understand the core concurrency concepts and how to apply them.
    88.1k 179.3k
    DOWNLOAD
    Core CSS: Part II
    Covers Core principles of CSS that will expand and strengthen your professional ability to work with CSS. Part two of three.
    72.3k 137.8k
    DOWNLOAD
    Getting Started with Eclipse
    Gives insights on Eclipse, the leading IDE for Java, which has a rich ecosystem of plug-ins and an open-source framework that supports other languages.
    72k 182.6k
    {{ card.title }}
    {{card.downloads | formatCount }} {{card.views | formatCount }}

    The best of DZone straight to your inbox.

    SEE AN EXAMPLE
    Please provide a valid email address.

    Thanks for subscribing!

    Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
    Subscribe

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}