ReactJs 18 new React Hooks, new Suspense, and new Memo
Things are blowing up in the React community lately! Between the suspense we had for a few months, Create React App v2, Hooks, Memo -- React developers new and old have their plates full with new toys to play with. I finally got some time to dig into the new React.memo()
, React.lazy()
and <Suspense />
APIs, as well as the proposed Hooks API.
PureComponent for Functional Components
A new technique to memoize! React.memo()
is a HOC that prevents a component from rendering on props change if the props are the same. It basically runs a shallow equal on the props in the shouldComponentUpdate()
lifecycle, but for functional components that don’t have access to it (without switching to a class).
And if the props contain complex objects, we can add a function inside the component to check:
This is a great performance gain for component and design systems that rely on functional components for rendering lower-level UI elements.
A callback “cache”
There’s also a new hook implemented that uses the same memoization logic on functions. It prevents the function from being called again unless it’s parameters (or variables you specify) change:
The suspense is over 🌟
The first thing I wanted to dig into was Suspense, since it's actually implemented (if not incomplete). After watching Dan's incredible talk about Suspense at ReactFest 2018 in March, I was excited that React was making lazy loading a priority enough to incoporate it into their API. Rather than relying on a library like react-loadable or configurations in Webpack, I can just simply:
Not only do I get the benefit of deferring the loading of my components bundle (making the app load initially faster), but I can also plug in any loading component. It makes illusions like skeleton screens an effortless task.
You can see a live example on CodeSandbox:
Hooks
Recently React has proposed a new, more functional way of handling state using "hooks", rather than relying on the lifecycle methods of a React component. You can find the entire proposal in the React docs here.
Using them is simple, and offers a lower LOC with functional components compared to the class alternative.
To handle any side effects in the component, throw in a useEffect()
inside the functional component to run code on each state change / re-render.
One of the best part of hooks is their functional nature (FP FTW). You can extract the hook and effect into a separate function, and re-use that hook across multiple components in the app.
Hooks = Less Compiled Code
One of the best parts of the addition of hooks is the ability to abandon classes for stateful logic in favor of more efficient functions. If you've ever looked at most compiled JS code, because of the way classes work (being syntactic sugar over prototypes), using a class in your app bloats your code immensely with polyfills.
This class:
compiles to:
In contrast, if you use a function (unless it's a ES6 arrow function), it compiles just as it appears -- since functions are so broadly supported (being so primitive/early JS API). Even when you take array destructuring into account, the code is still less than the class, while being able to use state:
A more composable future for React
It's been nice to see the improvements to the React API over the past year. The team does a fantastic job of maintaining legacy API and not breaking apps (Facebook still using React.createElement
), and the addition of new features all address key issues developers have. I can't tell you how many times I've had to take a functional component and convert it into a class just for a single stateful boolean, where now I'll be able to just drop a hook in the top of the function (and memoize it to get the same perf as PureComponent!).