2

I am using React Compiler v1.0 in a React 19 application. I have specific components where I intentionally want fresh calculations on every render for correctness reasons but the React Compiler is automatically memoizing them.

React Compiler automatically memoizes all values, but I have legitimate use cases where values should not be cached between renders. The "use no memo" directive disables the entire component's optimization, which throws away beneficial memoization for other parts of the component.

function RealtimeMonitor({ sensorId }) {
  // This timestamp MUST be fresh on every render for accurate monitoring
  const currentTimestamp = Date.now();
  
  // This should be memoized (expensive calculation)
  const processedData = processLargeSensorDataset(sensorId);
  
  // This should NOT be memoized (randomization needed each render)
  const chartColor = getRandomChartColor();
  
  return (
    <div>
      <Chart 
        data={processedData} 
        color={chartColor}
        timestamp={currentTimestamp}
      />
      <LastUpdated time={currentTimestamp} />
    </div>
  );
}

How can I mark specific values or computations within a component as "volatile" or "non-memoizable" while allowing React Compiler to continue optimizing the rest of the component? Is there a special directive, recommended pattern, or compiler configuration that allows mixing memoized and non-memoized computations without disabling optimization for the entire component?

1 Answer 1

2

How can I mark specific values or computations within a component as "volatile" or "non-memoizable" while allowing React Compiler to continue optimizing the rest of the component? Is there a special directive, recommended pattern, or compiler configuration that allows mixing memoized and non-memoized computations without disabling optimization for the entire component?

Based on the current usage documentation it would appear you have only Function-level directives (Place directives at the beginning of a function to control its compilation) and Module-level directives (Place directives at the top of a file to affect all functions in that module). So the answer appears to be "No, it's all or nothing" at function or module scope.

If you have need to customize the compiler behavior more granularly than the function/module scope then the option I see is to opt-out of compiler optimization and manually optimize any computations yourself just as you would have done before using React-Compiler, i.e. using the useMemo or useCallback hooks, or memo Higher-Order Component (and any other memoization tools/libraries).

Example opting the component function out of compilation and memoizing the expensive function call:

function RealtimeMonitor({ sensorId }) {
  // Opt-out of compiler optimizations to allow dynamic values
  // to be computed each render. We don't want them to be memoized.
  "use no memo";

  // This timestamp MUST be fresh on every render for accurate monitoring
  const currentTimestamp = Date.now();
  
  // This should be memoized (expensive calculation)
  const processedData = React.useMemo(
    () => processLargeSensorDataset(sensorId),
    [sensorId]
  );
  
  // This should NOT be memoized (randomization needed each render)
  const chartColor = getRandomChartColor();
  
  return (
    <div>
      <Chart 
        data={processedData} 
        color={chartColor}
        timestamp={currentTimestamp}
      />
      <LastUpdated time={currentTimestamp} />
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.