React 16.0—16.9 new features for every day use
This is a short cheat sheet for developers migrating from React 15 to React 16, or from earlier 16.x versions to 16.9. It focuses on features you’ll use often.
Returning multiple elements from components with fragments
Splitting UI into small reusable components may lead to creation of unnecessary DOM elements, like when you need to return multiple elements from a component. React 16 has several options to avoid that:
Note The short syntax may not be supported by the tools you’re using.
Returning strings and numbers from components
In React 16 components can return strings and numbers. This is useful for components that don’t need any markup, like internationalization or formatting:
Cancelling setState() to avoid rerendering
In React 15 it wasn’t possible to cancel setState()
and avoid rerendering, if your next state was based on the previous state. In React 16 you could return null
in setState()
’s callback:
In this example calling handleChange()
with the same city name as in the state won’t cause a rerender.
Avoiding prop drilling with the official context API (16.3)
Prop drilling is when you’re passing some data to a deeply nested component using a prop, so you have to add this prop to each layer of your React component tree between a component that owns the data and a component that consumes it.
That’s a lot of boilerplate code! With the context API we can access our theme props anywhere in the component tree:
Check out an example on CodeSandbox.
Updating state based on props with getDerivedStateFromProps() (16.3)
The getDerivedStateFromProps()
lifecycle method is a replacement for componentWillReceiveProps()
. It’s useful when you have a prop with a default value for a state property, but you want to reset the state when that prop changes. For example, a modal that has a prop that says if it’s initially open, and a state that says if a modal is open now:
The getDerivedStateFromProps()
method is called when a component is created and when it receives new props, so you don’t have to convert props to state twice (on initialization and in componentWillReceiveProps
).
Rerendering function components on props change with React.memo() (16.6)
React.memo() does the same for function components as PureComponent does for class components: only rerenders the component if its props change.
Easier access to context in class components with contextType (16.6)
Class.contextType simplifies access to the React context in class components:
Using state in function components with hooks (16.8)
Hooks allows you to use state and other React features in function components. Often code with hooks is simpler than with classes.
For example, we can use state with the useState hook:
Check out this example on CodeSandbox.
Other hooks are:
- useEffect to manage side effects;
- useContext to access React context;
- useReducer to manage complex state;
- useCallback to memoize functions;
- useMemo to memoize values.
- useRef to persist values between renders;
- useImperativeHandle to forward imperative methods;
- useLayoutEffect to manage side effects synchronously;
- useDebugValue to debug custom hooks.
And you can create your own hooks to share logic between function components.
Other new features
React 16 has many other features that are useful in some cases:
- error boundaries;
- portals;
- forwarding refs (16.3);
- getSnapshotBeforeUpdate() lifecycle method (16.3);
- StrictMode component (16.3);
- pointer events (16.4);
- profiler (16.5);
- React.lazy (16.6);
- static getDerivedStateFromError() (16.6).
- act() and async act() for testing (16.8 and 16.9).
component for performance measurements (16.9).
I also highly recommend Nik Graf’s course on React 16 and Kent C. Dodds’s course on hooks on Egghead.