Skip to content

noReactObjectTypeAsDefaultProp (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noReactObjectTypeAsDefaultProp": "error"
}
}
}
}

Disallow array, object, and function values as default props in React components.

In React, a default prop value like { items = [] } is created every time the component renders. Arrays, objects, and functions are new values each time, even when they look the same. React then thinks the prop changed, so it may re-render the component more than needed, or re-run hooks like useEffect that depends on the prop.

Numbers, strings, and other primitives are fine, because they stay the same among renders.

function Component({ items = [] }) {
return items;
}
code-block.js:1:30 lint/nursery/noReactObjectTypeAsDefaultProp ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Every render creates an array literal here.

> 1 │ function Component({ items = [] }) {
^^
2 │ return items;
3 │ }

React sees this as a different value each render, so the component may re-render more than needed.

Move this value to a constant outside the component and use that as the default.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const Component = ({ config = {} }) => config;
code-block.js:1:31 lint/nursery/noReactObjectTypeAsDefaultProp ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Every render creates an object literal here.

> 1 │ const Component = ({ config = {} }) => config;
^^
2 │

React sees this as a different value each render, so the component may re-render more than needed.

Move this value to a constant outside the component and use that as the default.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const EMPTY_ITEMS = [];
function Component({ items = EMPTY_ITEMS }) {
return items;
}
function Component({ count = 0, label = "default" }) {
return count;
}