Copy import React from 'react'
Copy import ReactDOM from ' react-dom '
Copy class Hello extends React.Component {
Copy render () {
return < div className = ' message-box ' >
Hello { this . props . name }
</ div >
}
}
Copy const el = document . body
ReactDOM . render ( < Hello name = ' John ' />, el ) Use the React.js jsfiddlearrow-up-right to start hacking. (or the unofficial jsbinarrow-up-right )
Import multiple exports
Copy import React , { Component } from ' react '
import ReactDOM from ' react-dom '
Copy class Hello extends Component { {: .-setup}
Use this.props to access properties passed to the component.
See: Propertiesarrow-up-right
Use states (this.state) to manage dynamic data.
With Babelarrow-up-right you can use proposal-class-fieldsarrow-up-right and get rid of constructor
See: Statesarrow-up-right
As of React v16.2.0, fragments can be used to return multiple children without adding extra wrapping nodes to the DOM.
Nest components to separate concerns.
See: Composing Componentsarrow-up-right
{: data-line="2"}
Children are passed as the children property.
Setting default props
See: defaultPropsarrow-up-right
Setting default state
{: data-line="4"}
Set the default state in the constructor().
And without constructor using Babelarrow-up-right with proposal-class-fieldsarrow-up-right .
See: Setting the default statearrow-up-right
Other components
{: .-three-column}
Functional components
Functional components have no state. Also, their props are passed as the first parameter to a function.
See: Function and Class Componentsarrow-up-right
Pure components
Performance-optimized version of React.Component. Doesn't rerender if props/state hasn't changed.
See: Pure componentsarrow-up-right
These methods and properties are available for Component instances.
See: Component APIarrow-up-right
Set initial the state on constructor(). Add DOM event handlers, timers (etc) on componentDidMount(), then remove them on componentWillUnmount().
componentDidUpdate (prevProps, prevState, snapshot)
Use setState() here, but remember to compare props
shouldComponentUpdate (newProps, newState)
Skips render() if returns false
componentDidUpdate (prevProps, prevState)
Called when parents change properties and .setState(). These are not called for initial renders.
See: Component specsarrow-up-right
{: .-two-column}
Hooks are a new addition in React 16.8.
See: Hooks at a Glancearrow-up-right
Declaring multiple state variables
If you're familiar with React class lifecycle methods, you can think of useEffect Hook as componentDidMount, componentDidUpdate, and componentWillUnmount combined.
By default, React runs the effects after every render โ including the first render.
Building your own hooks
Define FriendStatus
Effects may also optionally specify how to "clean upโ after them by returning a function.
Use FriendStatus
See: Building Your Own Hooksarrow-up-right
Hooks API Reference
Also see: Hooks FAQarrow-up-right
Basic Hooks
value returned from React.createContext
Full details: Basic Hooksarrow-up-right
Additional Hooks
useReducer(reducer, initialArg, init)
useCallback(() => { ... })
useImperativeHandle(ref, () => { ... })
identical to useEffect, but it fires synchronously after all DOM mutations
display a label for custom hooks in React DevTools
Full details: Additional Hooksarrow-up-right
Allows access to DOM nodes.
See: Refs and the DOMarrow-up-right
Pass functions to attributes like onChange.
See: Eventsarrow-up-right
Transferring props
{: .-setup}
Propagates src="..." down to the sub-component.
See Transferring propsarrow-up-right
There are more, but these are most common.
See: React top-level APIarrow-up-right
Style shorthand
See: Inline stylesarrow-up-right
See: Dangerously set innerHTMLarrow-up-right
Always supply a key property.
Short-circuit evaluation
Returning multiple elements
You can return multiple elements as arrays or fragments.
Arrays
{: data-line="3,4,5,6"}
Fragments
See: Fragments and stringsarrow-up-right
Returning strings
You can return just a string.
See: Fragments and stringsarrow-up-right
Catch errors via componentDidCatch. (React 16+)
See: Error handling in React 16arrow-up-right
This renders this.props.children into any location in the DOM.
See: Portalsarrow-up-right
Use ReactDOM.hydrate instead of using ReactDOM.render if you're rendering over the output of ReactDOMServerarrow-up-right .
See: Hydratearrow-up-right
Property validation
See: Typechecking with PropTypesarrow-up-right
| any | Anything |
Basic
| string | | | number | | | func | Function | | bool | True or false |
Enum
| oneOf(any) | Enum types | | oneOfType(type array) | Union |
Array
| array | | | arrayOf(...) | |
Object
| object | | | objectOf(...) | Object with values of a certain type | | instanceOf(...) | Instance of a class | | shape(...) | |
Elements
| element | React element | | node | DOM node |
Required
| (ยทยทยท).isRequired | Required |
Enumerables (oneOf)
Arrays and objects
Use .array[Of], .object[Of], .instanceOf, .shape.
Custom validation
React v0.14 cheatsheet Legacy version