Bats plus React useMemo
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 descriptionExperiment 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.