---
title: React.js
category: React
layout: 2017/sheet
---
{%raw%}
Example
-------
{: .-left-reference}
### Basic example
```jsx
class Hello extends React.Component {
render () {
return
Hello {this.props.name}
}
}
```
```jsx
const el = document.body
ReactDOM.render(, el)
```
Use the [React.js jsfiddle](http://jsfiddle.net/reactjs/69z2wepo/) to start hacking. (or the unofficial [jsbin](http://jsbin.com/yafixat/edit?js,output)).
### Try it
[Open in jsbin](http://jsbin.com/yafixat/edit?js,output)
{: target="_blank"}
Components
----------
{: .-three-column}
### Class components
```jsx
class MyComponent extends React.Component {
render () {
return
Hello {this.props.name}
}
}
```
```jsx
const el = document.body
ReactDOM.render(, el)
```
### Functional components
```jsx
function MyComponent ({ name }) {
return
Hello {name}
}
```
Functional components have no state. Also, their `props` are passed as the first parameter to a function.
### Nesting
```jsx
class Info extends React.Component {
render () {
const { avatar, username } = this.props
return
}
}
```
Nest components to separate concerns. See: [multiple components](http://facebook.github.io/react/docs/multiple-components.html)
### Component API
```jsx
this.forceUpdate()
```
```jsx
this.setState({ ... })
this.replaceState({ ... })
```
```jsx
this.state
this.props
```
These methods and properies are available for `Component` instances. See [Component API](http://facebook.github.io/react/docs/component-api.html).
### Properties
```html
```
```jsx
class Video extends React.Component {
render () {
this.props.fullscreen
/* ... */
}
}
```
Use [props](https://facebook.github.io/react/docs/tutorial.html#using-props) (`this.props`) to access parameters passed from the parent.
### States
```jsx
this.setState({ username: 'rstacruz' })
```
```jsx
render () {
this.state.username
/* ... */
}
```
Use states (`this.state`) to manage dynamic data.
See [States](https://facebook.github.io/react/docs/tutorial.html#reactive-state).
### Setting default props
```jsx
Hello.defaultProps = {
color: 'blue'
}
```
See [defaultProps](https://facebook.github.io/react/docs/react-component.html#defaultprops).
### Setting default state
```jsx
class Hello extends React.Component {
constructor (props) {
super(props)
this.state = { visible: true }
}
}
```
Lifecycle
---------
{: .-two-column}
### Mounting
| `constructor` _(props)_ | Before rendering [#](https://facebook.github.io/react/docs/react-component.html#constructor) |
| `componentWillMount()` | _Don't use this_ [#](https://facebook.github.io/react/docs/react-component.html#componentwillmount) |
| `render()` | Render [#](https://facebook.github.io/react/docs/react-component.html#render) |
| `componentDidMount()` | After rendering (DOM available) [#](https://facebook.github.io/react/docs/react-component.html#componentdidmount) |
| `componentWillUnmount()` | Before DOM removal [#](https://facebook.github.io/react/docs/react-component.html#componentwillunmount) |
Set initial the state on `constructor()`.
Add DOM event handlers, timers (etc) on `componentDidMount()`, then remove them on `componentWillUnmount()`.
### Updating
| `componentWillReceiveProps` *(newProps)* | Use `setState()` here |
| `shouldComponentUpdate` *(newProps, newState)* | Skips `render()` if returns false |
| `componentWillUpdate` *(newProps, newState)* | Can't use `setState()` here |
| `render()` | Render |
| `componentDidUpdate` *(prevProps, prevState)* | Operate on the DOM here |
Called when parents change properties and `.setState()`. These are not called for initial renders. See [reference](http://facebook.github.io/react/docs/component-specs.html#updating-componentwillreceiveprops).
DOM nodes
---------
{: .-two-column}
### References
```jsx
class MyComponent extends React.Component {
render () {
return
```
Also see
--------
{: .-one-column}
This reference was made for React v15.
* [React website](http://facebook.github.io/react) _(facebook.github.io)_
* [Animations](http://facebook.github.io/react/docs/animation.html) _(facebook.github.io)_
{%endraw%}