Documentation
Rules
no-unstable-default-props

no-unstable-default-props

Rule category

Perf.

What it does

Prevents usage of referential-type values as default props in object destructuring.

Why is this bad?

When using object destructuring syntax you can set the default value for a given property if it does not exist. If you set the default value to one of the values that is compared by identity, then each time the destructuring is evaluated, the JS engine will create a new, distinct value in the destructured variable.

This harms performance as it means that React will have to re-evaluate hooks and re-render memoized components more often than necessary.

To fix the violations, the easiest way is to use a referencing variable in module scope instead of using the literal values.

Examples

Failing

import React from "react";
 
interface ExampleProps {
  items: string[];
}
 
function Example({ items = [] }: ExampleProps) {
  //                       ^^
  //                       - found a/an 'Array literal' as default prop. This could lead to potential infinite render loop in React. Use a variable instead of 'Array literal'.
  return null;
}
import React from "react";
 
interface ExampleProps {
  items: Record<string, string>;
}
 
function Example({ items = {} }: ExampleProps) {
  //                       ^^
  //                       - found a/an 'Object literal' as default prop. This could lead to potential infinite render loop in React. Use a variable instead of 'Object literal'.
  return null;
}
import React from "react";
 
interface ExampleProps {
  onClick: () => void;
}
 
function Example({ onClick = () => {} }: ExampleProps) {
  //                         ^^^^^^^^
  //                         - found a/an 'arrow function expression' as default prop. This could lead to potential infinite render loop in React. Use a variable instead of 'arrow function expression'.
  return null;
}
import React from "react";
 
interface ExampleProps {
  items: string[];
}
 
function Example(props: ExampleProps) {
  const { items = [] } = props;
  //              ^^
  //              - found a/an 'Array literal' as default prop. This could lead to potential infinite render loop in React. Use a variable instead of 'Array literal'.
  return null;
}

Passing

import React from "react";
 
const emptyArray: string[] = [];
 
interface ExampleProps {
  items: string[];
}
 
function Example({ items = emptyArray }: ExampleProps) {
  return null;
}
import React from "react";
 
const emptyObject = {};
 
interface ExampleProps {
  items: Record<string, string>;
}
 
function Example({ items = emptyObject }: ExampleProps) {
  return null;
}
import React from "react";
 
const noop = () => {};
 
interface ExampleProps {
  onClick: () => void;
}
 
function Example({ onClick = noop }: ExampleProps) {
  return null;
}
import React from "react";
 
const emptyArray: string[] = [];
 
interface ExampleProps {
  items: string[];
}
 
function Example(props: ExampleProps) {
  const { items = emptyArray } = props;
 
  return null;
}
import React from "react";
 
interface ExampleProps {
  num: number;
  str: string;
  bool: boolean;
}
 
// @log: Primitives are all compared by value, so are safe to be inlined
function Example({ num = 3, str = "foo", bool = true }: ExampleProps) {
  return null;
}