The issue involves clarifying or fixing behavior around purity rules and ref initialization.
The issue highlights a discrepancy between expected and actual behavior when initializing refs with impure functions using `=== null`. It seems to be a documentation or implementation issue rather than a lint bug. The main task is to clarify or adjust the behavior to align with React's purity rules.
See the link for playground.
Basically, the ref-in-render rule doc states that this very specific access is allowed:
Example 1:
// ✅ Lazy initialization of ref value
function Component() {
const ref = useRef(null);
// Initialize only once on first use
if (ref.current === null) {
ref.current = expensiveComputation(); // OK - lazy initialization
}
However, if expensiveComputation is impure, it still fires.
Example 2:
if (ref.current === null) {
ref.current = Date.now(); // purity rule
And of course, useState(() => Date.now()) is fine. The react docs say
// It only runs on
Claim this issue to let others know you're working on it. You'll earn 20 points when you complete it!