React Hooks, Suspense, and Memo

React Hooks, Suspense, and Memo

Things are blowing up in the React community lately! Between the&nbsp;<em>suspense</em>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&nbsp;React.memo(),&nbsp;React.lazy()&nbsp;and&nbsp;&lt;Suspense /&gt;&nbsp;APIs, as well as the proposed Hooks API.

Things are blowing up in the React community lately! Between the suspensewe 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).

const MyComponent = React.memo(function MyComponent(props) {
  /* render using props */
});

And if the props contain complex objects, we can add a function inside the component to check:

function MyComponent(props) {
  /* render using props */
}
function areEqual(prevProps, nextProps) {
  /*
  return true if passing nextProps to render would return
  the same result as passing prevProps to render,
  otherwise return false
  */
}
export default React.memo(MyComponent, areEqual);

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:

const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);

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:

const OtherComponent = React.lazy(() => import('./OtherComponent'));

function MyComponent() { return ( <div> <Suspense fallback={<div>Loading...</div>}> <OtherComponent /> </Suspense> </div> ); }

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.

function YourComponent({ text }) {
  const [ theText, updateText] = useState(text)
  const changeText = ({ target: { value } }) => {
    updateText(value)
  }
  return(
    <button onClick={() => changeText}>
      {theText}
    </button>
  )
}

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:

class Test extends React {
  constructor() {
    super()
    this.state = {}
  }
  render() {
    return <div>Test</div>
  }
}

compiles to:

"use strict";

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass._proto = superClass; }

var Test = function (_React) { _inherits(Test, _React);

function Test() { _classCallCheck(this, Test);

var _this = _possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this));

_this.state = {};
return _this;

}

_createClass(Test, [{ key: "render", value: function render() { return React.createElement( "div", null, "Test" ); } }]);

return Test; }(React);

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:

function Test(props) {
  const [counter, increment] = useState(0);
  return <h1>Hello</h1>;
}
"use strict";

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arrSymbol.iterator, _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i"return"; } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

function Test(props) { var _useState = useState(0), _useState2 = _slicedToArray(_useState, 2), counter = _useState2[0], increment = _useState2[1];

return React.createElement( "h1", null, "Hello" ); }

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!).


By : Ryosuke


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

ReactJS vs Angular vs Vue: Best Javascript Framework For Your Project

ReactJS vs Angular vs Vue: Best Javascript Framework For Your Project. This video covers the key differences between ReactJS, Angular and Vue with respect to the following: Use case, Performance, Data binding, Scripting language, Testing, Community support, Growth curve

What is JavaScript – All You Need To Know About JavaScript

In this article on what is JavaScript, we will learn the basic concepts of JavaScript.

JavaScript Tutorial: if-else Statement in JavaScript

This JavaScript tutorial is a step by step guide on JavaScript If Else Statements. Learn how to use If Else in javascript and also JavaScript If Else Statements. if-else Statement in JavaScript. JavaScript's conditional statements: if; if-else; nested-if; if-else-if. These statements allow you to control the flow of your program's execution based upon conditions known only during run time.