React

React is the entry point to the React library. If you're using one of the prebuilt packages it's available as a global; if you're using CommonJS modules you can require() it.

React.Component

class Component

This is the base class for React Components when they're defined using ES6 classes. See Reusable Components for how to use ES6 classes with React. For what methods are actually provided by the base class, see the Component API.

React.createClass

ReactClass createClass(object specification)

Create a component class, given a specification. A component implements a render method which returns one single child. That child may have an arbitrarily deep child structure. One thing that makes components different than standard prototypal classes is that you don't need to call new on them. They are convenience wrappers that construct backing instances (via new) for you.

For more information about the specification object, see Component Specs and Lifecycle.

React.createElement

ReactElement createElement(
  string/ReactClass type,
  [object props],
  [children ...]
)

Create and return a new ReactElement of the given type. The type argument can be either an html tag name string (eg. 'div', 'span', etc), or a ReactClass (created via React.createClass).

React.cloneElement

ReactElement cloneElement(
  ReactElement element,
  [object props],
  [children ...]
)

Clone and return a new ReactElement using element as the starting point. The resulting element will have the original element's props with the new props merged in shallowly. New children will replace existing children. Unlike React.addons.cloneWithProps, key and ref from the original element will be preserved. There is no special behavior for merging any props (unlike cloneWithProps). See the v0.13 RC2 blog post for additional details.

React.createFactory

factoryFunction createFactory(
  string/ReactClass type
)

Return a function that produces ReactElements of a given type. Like React.createElement, the type argument can be either an html tag name string (eg. 'div', 'span', etc), or a ReactClass.

React.isValidElement

boolean isValidElement(* object)

Verifies the object is a ReactElement.

React.DOM

React.DOM provides convenience wrappers around React.createElement for DOM components. These should only be used when not using JSX. For example, React.DOM.div(null, 'Hello World!')

React.PropTypes

React.PropTypes includes types that can be used with a component's propTypes object to validate props being passed to your components. For more information about propTypes, see Reusable Components.

React.Children

React.Children provides utilities for dealing with the this.props.children opaque data structure.

React.Children.map

array React.Children.map(object children, function fn [, object thisArg])

Invoke fn on every immediate child contained within children with this set to thisArg. If children is a keyed fragment or array it will be traversed: fn will never be passed the container objects. If children is null or undefined returns null or undefined rather than an array.

React.Children.forEach

React.Children.forEach(object children, function fn [, object thisArg])

Like React.Children.map() but does not return an array.

React.Children.count

number React.Children.count(object children)

Return the total number of components in children, equal to the number of times that a callback passed to map or forEach would be invoked.

React.Children.only

object React.Children.only(object children)

Return the only child in children. Throws otherwise.

React.Children.toArray

array React.Children.toArray(object children)

Return the children opaque data structure as a flat array with keys assigned to each child. Useful if you want to manipulate collections of children in your render methods, especially if you want to reorder or slice this.props.children before passing it down.

ReactDOM

The react-dom package provides DOM-specific methods that can be used at the top level of your app and as an escape hatch to get outside of the React model if you need to. Most of your components should not need to use this module.

ReactDOM.render

render(
  ReactElement element,
  DOMElement container,
  [function callback]
)

Render a ReactElement into the DOM in the supplied container and return a reference to the component (or returns null for stateless components).

If the ReactElement was previously rendered into container, this will perform an update on it and only mutate the DOM as necessary to reflect the latest React component.

If the optional callback is provided, it will be executed after the component is rendered or updated.

ReactDOM.render() controls the contents of the container node you pass in. Any existing DOM elements inside are replaced when first called. Later calls use React’s DOM diffing algorithm for efficient updates.

ReactDOM.render() does not modify the container node (only modifies the children of the container). In the future, it may be possible to insert a component to an existing DOM node without overwriting the existing children.

ReactDOM.render() currently returns a reference to the root ReactComponent instance. However, using this return value is legacy and should be avoided because future versions of React may render components asynchronously in some cases. If you need a reference to the root ReactComponent instance, the preferred solution is to attach a callback ref to the root element.

ReactDOM.unmountComponentAtNode

boolean unmountComponentAtNode(DOMElement container)

Remove a mounted React component from the DOM and clean up its event handlers and state. If no component was mounted in the container, calling this function does nothing. Returns true if a component was unmounted and false if there was no component to unmount.

ReactDOM.findDOMNode

DOMElement findDOMNode(ReactComponent component)

If this component has been mounted into the DOM, this returns the corresponding native browser DOM element. This method is useful for reading values out of the DOM, such as form field values and performing DOM measurements. In most cases, you can attach a ref to the DOM node and avoid using findDOMNode at all. When render returns null or false, findDOMNode returns null.

findDOMNode() is an escape hatch used to access the underlying DOM node. In most cases, use of this escape hatch is discouraged because it pierces the component abstraction.

findDOMNode() only works on mounted components (that is, components that have been placed in the DOM). If you try to call this on a component that has not been mounted yet (like calling findDOMNode() in render() on a component that has yet to be created) an exception will be thrown.

findDOMNode() cannot be used on stateless components.

ReactDOMServer

The react-dom/server package allows you to render your components on the server.

ReactDOMServer.renderToString

string renderToString(ReactElement element)

Render a ReactElement to its initial HTML. This should only be used on the server. React will return an HTML string. You can use this method to generate HTML on the server and send the markup down on the initial request for faster page loads and to allow search engines to crawl your pages for SEO purposes.

If you call ReactDOM.render() on a node that already has this server-rendered markup, React will preserve it and only attach event handlers, allowing you to have a very performant first-load experience.

ReactDOMServer.renderToStaticMarkup

string renderToStaticMarkup(ReactElement element)

Similar to renderToString, except this doesn't create extra DOM attributes such as data-react-id, that React uses internally. This is useful if you want to use React as a simple static page generator, as stripping away the extra attributes can save lots of bytes.

React (Virtual) DOM Terminology

In React's terminology, there are five core types that are important to distinguish: ReactElement / ReactElement Factory ReactNode

2016-06-23 03:32:07
Reconciliation

React's key design decision is to make the API seem like it re-renders the whole app on every update. This makes writing applications a lot easier but is also an incredible

2016-06-23 03:32:23
Tags and Attributes

Supported Elements React attempts to support all common elements in both HTML and SVG. Any lower case tag in JSX

2016-06-23 03:32:31
Web Components

Trying to compare and contrast React with WebComponents inevitably results in specious conclusions, because the two libraries are built to solve different problems. WebComponents

2016-06-23 03:32:38
Special Non-DOM Attributes

Beside DOM differences, React offers some attributes that simply don't exist in DOM.

2016-06-23 03:32:30
Event System

SyntheticEvent Your event handlers will be passed instances of SyntheticEvent, a cross-browser wrapper

2016-06-23 03:31:57
DOM Differences

React has implemented a browser-independent events and DOM system for performance and cross-browser compatibility reasons. We took the opportunity to clean up a few rough edges

2016-06-23 03:31:56