redline
LowReactunstable-context-value

New object as context value

New object as context value. Passes a new object to a context provider on every render, so every consumer re-renders.

This is how the comment reads inline, under the flagged line.

Why it matters

Inline value={{...}} creates a fresh object each render; React sees a change and re-renders the whole subtree.

What to do

Memoize the value with useMemo, or split the context.

Examples

Flagged
<Ctx.Provider value={{ theme, setTheme }}>
Better
const value = useMemo(() => ({ theme, setTheme }), [theme]);
<Ctx.Provider value={value}>

What it catches

It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.

Flagged

<ThemeContext.Provider value={{ theme, setTheme }}>

Left alone

<ThemeContext.Provider value={themeValue}>

Research

A new object or array passed as a context value. Every consumer re-renders on every render.

  • owner's notes

Turn it off or change it

In a config file for the CLI, or in the API's rules field:

{
  "rules": {
    "disable": [
      "unstable-context-value"
    ],
    "severity": {
      "unstable-context-value": "medium"
    }
  }
}

Read the source or report a false positive.