Skip to main content

What it flags

A useState value is changed in place (push, splice, sort, reverse, an assignment to a property or index) and then passed to its own setter. React compares by reference, so the re-render is skipped. The change is also seen through type and non-null assertions and bracket calls with a literal method name, such as items!['push'](item). The rule prefers silence when it cannot tell what ran:
  • A change to a value read through a getter, as in items.rows.push(row) where rows is declared with get, is not reported: the getter may return a new value on every read. A getter in an object literal whose only statement returns a plain property of the same object, such as return this.stored, is read through.
  • A ??=, ||= or &&= is skipped only when the initial object literal and every use of the state and its setter show that it cannot assign. A declared or asserted type is not enough. Reading that same property keeps the proof; any other use of the state, such as rendering a different property or passing the state to a function, ends it. ??= on the state’s length is also skipped when the initial value is an array literal or a built-in new Array(), which own their length, and the setter is only given the state back. A built-in typed array inherits its length, so there every use of the state must pass the same proof, where reading an element such as items[0] also keeps it.
  • A method that the file assigns on a binding or its const alias, as in work.forEach = later, is not treated as built in, even when the receiver is typed. A Map or Set method counts only when the default library declares it, not a local class with the same name.
  • A name assigned in code that may not run (one arm of if or ?:, a loop, case, try, callback or the right side of &&, || or ??) is not treated as the state value afterwards, unless both arms of an if or ?: leave it referring to the same value.
  • A name overwritten by destructuring, a loop head or a compound assignment such as ||= is not treated as the state value afterwards.
  • A let or var alias that any other function assigns is never treated as the state value, whether or not that function runs.
  • A mutation in one case reaches the next only when the first can fall through to it.
  • Adjacent if statements on the same unchanging identifier or its ! are read as agreeing on its value, and a guard on the switched value that cannot be decided keeps a change from running on to the next case.
  • A mutation inside a callback counts toward a later setter call for built-in synchronous callback methods such as forEach, map, Array.from and String.replace (including methods with no declaration, as on untyped values) and React’s startTransition, flushSync and unstable_batchedUpdates. It also counts when the code waits for the work: await load().then(...) (or catch, finally), await Promise.all(ids.map(async ...)) (or allSettled) and await (async () => {})(). A const list of promises counts when the very next statement is await Promise.all(tasks), and so does a promise that an inline callback returns to an awaited .then. Other callbacks may run later, including unawaited async ones, generators, library helpers and same-named methods declared by your own code or a library. A callback that both mutates and calls the setter is still reported. A callback of an array method called on a literal [], as in [].forEach(...), never runs and is skipped; any other list is assumed to have an element. Only inline callbacks are traced toward a later setter call: a mutation made only inside a separately declared one, as in const push = (value) => items.push(value); items.forEach(push); setItems(items), is missed.
  • Only an argument the method calls counts: the first for most methods, the second for Array.from, replace and replaceAll, and either of the first two for then. A thisArg is only passed along.
  • What awaited .then or async work changes reaches the caller after the wait, not a sibling in the same await. The exception is what an awaited async mapping or invocation does before its first await: its concise body, or its leading expression, variable and return statements. An if, loop, try or block ends that prefix. The rule assumes the mapped list is not empty. What it concludes from Promise.all or allSettled assumes standard promise machinery, including an unmodified constructor lookup: a listed promise whose constructor getter throws, on itself or through a redefined Promise.prototype, makes even allSettled reject before the other listed work has finished, and this is not detected.
  • A rejection handler (catch, or the second argument of then) does not see what the step before it changed, and a catch sees only what has changed by a point in its try that can throw: a call, new, await, yield, throw or loop.
  • A setter call in a callback that may run later is skipped when the state variable may be reassigned before then: after the callback is passed on, from another function, or in a loop around it.
  • A let or var setter that is reassigned is skipped unless every reassignment is ||= or ??= (which keep a function) or a later statement in a block that is running the setter call.

How to review or fix

Set a copy instead.

Configuration

Set the rule to "off" in loadwise.config.json to drop its findings: