Skip to main content
100%

Bats plus React useMemo

✓ Published0🌍 Public
Ttanvee gujral
Last edited Mar 5, 2020
Created on Mar 4, 2020

This example demonstrates how React’s `useMemo` hook caches expensive computations, affecting performance when a counter increments. It displays a number `I` that increases on button click, alongside calculated prime numbers and the time taken to compute them. The code uses `React.useState` and `React.useMemo` within a functional component, and a custom `doPointlessComputationsWithBlocking` function from intensive.js performs blocking prime-number calculations. The `useMemo` dependency array is empty, so the heavy computation runs once on initial render, highlighting how memoization prevents repeated work during subsequent state updates.

AI-generated description

Experiment with React useMemo

With useMemo, the components will only get updated if their data changes. In this case, the data is an array. If useMemo takes in this array, all the bats will update on a change instead of the desired one bat.

MIT Licensed

Similar vizzes