This blog site has been archived. Go toreact.dev/blog to see the recent posts.
Today we’re releasing React 16.6 with a few new convenient features. A form of PureComponent/shouldComponentUpdate for function components, a way to do code splitting using Suspense and an easier way to consume Context from class components.
Check out the fullchangelog below.
React.memo
Class components can bail out from rendering when their input props are the same usingPureComponent
orshouldComponentUpdate
. Now you can do the same with function components by wrapping them inReact.memo
.
const MyComponent= React.memo(functionMyComponent(props){/* only rerenders if props change */});
React.lazy
: Code-Splitting withSuspense
You may have seenDan’s talk about React Suspense at JSConf Iceland. Now you can use the Suspense component to docode-splitting by wrapping a dynamic import in a call toReact.lazy()
.
import React,{lazy, Suspense}from'react';const OtherComponent=lazy(()=>import('./OtherComponent'));functionMyComponent(){return(<Suspensefallback={<div>Loading...</div>}><OtherComponent/></Suspense>);}
The Suspense component will also allow library authors to start building data fetching with Suspense support in the future.
Note: This feature is not yet available for server-side rendering. Suspense support will be added in a later release.
static contextType
InReact 16.3 we introduced the official Context API as a replacement to the previousLegacy Context API.
const MyContext= React.createContext();
We’ve heard feedback that adopting the new render prop API can be difficult in class components. So we’ve added a convenience API toconsume a context value from within a class component.
classMyClassextendsReact.Component{static contextType= MyContext;componentDidMount(){let value=this.context;/* perform a side-effect at mount using the value of MyContext */}componentDidUpdate(){let value=this.context;/* ... */}componentWillUnmount(){let value=this.context;/* ... */}render(){let value=this.context;/* render something based on the value of MyContext */}}
static getDerivedStateFromError()
React 16 introducedError Boundaries for handling errors thrown in React renders. We already had thecomponentDidCatch
lifecycle method which gets fired after an error has already happened. It’s great for logging errors to the server. It also lets you show a different UI to the user by callingsetState
.
Before that is fired, we rendernull
in place of the tree that threw an error. This sometimes breaks parent components that don’t expect their refs to be empty. It also doesn’t work to recover from errors on the server since theDid
lifecycle methods don’t fire during server-side rendering.
We’re adding another error method that lets you render the fallback UI before the render completes. See the docs forgetDerivedStateFromError()
.
Note:
getDerivedStateFromError()
is not yet available for server-side rendering. It is designed to work with server-side rendering in a future release. We’re releasing it early so that you can start preparing to use it.
In16.3 we introduced theStrictMode
component. It lets you opt-in to early warnings for patterns that might cause problems in the future.
We’ve added two more APIs to the list of deprecated APIs inStrictMode
. If you don’t useStrictMode
you don’t have to worry; these warning won’t fire for you.
contextType
API makes this a bit easier.If you’re having trouble upgrading, we’d like to hear your feedback.
React v16.6.0 is available on the npm registry.
To install React 16 with Yarn, run:
yarnadd react@^16.6.0 react-dom@^16.6.0
To install React 16 with npm, run:
npminstall--save react@^16.6.0 react-dom@^16.6.0
We also provide UMD builds of React via a CDN:
<scriptcrossoriginsrc="https://unpkg.com/react@16/umd/react.production.min.js"></script><scriptcrossoriginsrc="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
Refer to the documentation fordetailed installation instructions.
React.memo()
as an alternative toPureComponent
for functions. (@acdlite in#13748)React.lazy()
for code splitting components. (@acdlite in#13885)React.StrictMode
now warns about legacy context API. (@bvaughn in#13760)React.StrictMode
now warns aboutfindDOMNode
. (@sebmarkbage in#13841)unstable_AsyncMode
tounstable_ConcurrentMode
. (@trueadm in#13732)unstable_Placeholder
toSuspense
, anddelayMs
tomaxDuration
. (@gaearon in#13799 and@sebmarkbage in#13922)contextType
as a more ergonomic way to subscribe to context from a class. (@bvaughn in#13728)getDerivedStateFromError
lifecycle method for catching errors in a future asynchronous server-side renderer. (@bvaughn in#13746)<Context>
is used instead of<Context.Consumer>
. (@trueadm in#13829)window.event
in development. (@sergei-startsev in#13697)React.memo()
. (@alexmckenley in#13855)contextType
. (@alexmckenley and@sebmarkbage in#13889)scheduler
. (@gaearon in#13683)requestAnimationFrame
earlier. (@acdlite in#13785)envify
transform to the package. (@mridgway in#13766)