React.js
Components
Components
Use the React.js jsfiddle to start hacking. (or the unofficial jsbin)
Import multiple exports
Properties
{: .-setup}
Use this.props
to access properties passed to the component.
See: Properties
States
Use states (this.state
) to manage dynamic data.
With Babel you can use proposal-class-fields and get rid of constructor
See: States
Nesting
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 Components
Children
{: data-line="2"}
Children are passed as the children
property.
Defaults
Setting default props
See: defaultProps
Setting default state
{: data-line="4"}
Set the default state in the constructor()
.
And without constructor using Babel with proposal-class-fields.
See: Setting the default state
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 Components
Pure components
Performance-optimized version of React.Component
. Doesn't rerender if props/state hasn't changed.
See: Pure components
Component API
These methods and properties are available for Component
instances.
See: Component API
Lifecycle
Mounting
Set initial the state on constructor()
. Add DOM event handlers, timers (etc) on componentDidMount()
, then remove them on componentWillUnmount()
.
Updating
componentDidUpdate
(prevProps, prevState, snapshot)
Use setState()
here, but remember to compare props
shouldComponentUpdate
(newProps, newState)
Skips render()
if returns false
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: Component specs
Hooks (New)
{: .-two-column}
State Hook
Hooks are a new addition in React 16.8.
See: Hooks at a Glance
Declaring multiple state variables
Effect hook
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
Hooks API Reference
Also see: Hooks FAQ
Basic Hooks
useState
(initialState)
useEffect
(() => { ... })
useContext
(MyContext)
value returned from React.createContext
Full details: Basic Hooks
Additional Hooks
useReducer
(reducer, initialArg, init)
useCallback
(() => { ... })
useMemo
(() => { ... })
useRef
(initialValue)
useImperativeHandle
(ref, () => { ... })
useLayoutEffect
identical to useEffect
, but it fires synchronously after all DOM mutations
useDebugValue
(value)
display a label for custom hooks in React DevTools
Full details: Additional Hooks
DOM nodes
References
Allows access to DOM nodes.
See: Refs and the DOM
DOM Events
Pass functions to attributes like onChange
.
See: Events
Other features
Transferring props
{: .-setup}
Propagates src="..."
down to the sub-component.
Top-level API
There are more, but these are most common.
See: React top-level API
JSX patterns
Style shorthand
See: Inline styles
Inner HTML
See: Dangerously set innerHTML
Lists
Always supply a key
property.
Conditionals
Short-circuit evaluation
New features
Returning multiple elements
You can return multiple elements as arrays or fragments.
Arrays
{: data-line="3,4,5,6"}
Fragments
Returning strings
You can return just a string.
Errors
Catch errors via componentDidCatch
. (React 16+)
See: Error handling in React 16
Portals
This renders this.props.children
into any location in the DOM.
See: Portals
Hydration
Use ReactDOM.hydrate
instead of using ReactDOM.render
if you're rendering over the output of ReactDOMServer.
See: Hydrate
Property validation
PropTypes
See: Typechecking with PropTypes
| 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 |
Basic types
Required types
Elements
Enumerables (oneOf)
Arrays and objects
Use .array[Of]
, .object[Of]
, .instanceOf
, .shape
.
Custom validation
Also see
React website (reactjs.org)
React cheatsheet (reactcheatsheet.com)
Awesome React (github.com)
React v0.14 cheatsheet Legacy version
Last updated
Was this helpful?