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?