useReactFunctionComponentDefinition
Summary
Section titled “Summary”- Rule available since:
v2.5.0 - Diagnostic Category:
lint/nursery/useReactFunctionComponentDefinition - This rule has an unsafe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Inspired from
react/function-component-definition
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useReactFunctionComponentDefinition": "error" } } }}Description
Section titled “Description”Enforce a specific function type for React function components.
This rule keeps function component definitions consistent. By default, named components must be written as function declarations.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const MyComponent = (props) => { return <div>{props.name}</div>;};code-block.jsx:1:7 lint/nursery/useReactFunctionComponentDefinition FIXABLE ━━━━━━━━━━━━━━━━━━━━━━
ℹ The React component MyComponent is defined as an arrow function.
> 1 │ const MyComponent = (props) => {
│ ^^^^^^^^^^^
2 │ return <div>{props.name}</div>;
3 │ };
ℹ Mixing component definition styles makes component declarations harder to scan.
ℹ Rewrite this component as a function declaration or configure `namedComponents` to allow an arrow function.
ℹ 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.
ℹ Unsafe fix: Use a function declaration for this component.
1 │ - const·MyComponent·=·(props)·=>·{
1 │ + function·MyComponent·(props)·{
2 2 │ return <div>{props.name}</div>;
3 │ - };
3 │ + }
4 4 │
function MyComponent(props) { return <div>{props.name}</div>;}Options
Section titled “Options”namedComponents
Section titled “namedComponents”The function style to enforce for named React components. Accepted values are:
"functionDeclaration"(default): Enforce function declarations."functionExpression": Enforce function expressions assigned to component bindings."arrowFunction": Enforce arrow functions assigned to component bindings.
"functionDeclaration"
Section titled “"functionDeclaration"”{ "linter": { "rules": { "nursery": { "useReactFunctionComponentDefinition": { "options": { "namedComponents": "functionDeclaration" } } } } }}Invalid
Section titled “Invalid”const MyComponent = (props) => { return <div>{props.name}</div>;};code-block.jsx:1:7 lint/nursery/useReactFunctionComponentDefinition FIXABLE ━━━━━━━━━━━━━━━━━━━━━━
ℹ The React component MyComponent is defined as an arrow function.
> 1 │ const MyComponent = (props) => {
│ ^^^^^^^^^^^
2 │ return <div>{props.name}</div>;
3 │ };
ℹ Mixing component definition styles makes component declarations harder to scan.
ℹ Rewrite this component as a function declaration or configure `namedComponents` to allow an arrow function.
ℹ 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.
ℹ Unsafe fix: Use a function declaration for this component.
1 │ - const·MyComponent·=·(props)·=>·{
1 │ + function·MyComponent·(props)·{
2 2 │ return <div>{props.name}</div>;
3 │ - };
3 │ + }
4 4 │
function MyComponent(props) { return <div>{props.name}</div>;}"functionExpression"
Section titled “"functionExpression"”{ "linter": { "rules": { "nursery": { "useReactFunctionComponentDefinition": { "options": { "namedComponents": "functionExpression" } } } } }}Invalid
Section titled “Invalid”function MyComponent(props) { return <div>{props.name}</div>;}code-block.jsx:1:10 lint/nursery/useReactFunctionComponentDefinition FIXABLE ━━━━━━━━━━━━━━━━━━━━━
ℹ The React component MyComponent is defined as a function declaration.
> 1 │ function MyComponent(props) {
│ ^^^^^^^^^^^
2 │ return <div>{props.name}</div>;
3 │ }
ℹ Mixing component definition styles makes component declarations harder to scan.
ℹ Rewrite this component as a function expression or configure `namedComponents` to allow a function declaration.
ℹ 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.
ℹ Unsafe fix: Use a function expression for this component.
1 │ - function·MyComponent(props)·{
1 │ + const·MyComponent·=·function·(props)·{
2 2 │ return <div>{props.name}</div>;
3 │ - }
3 │ + };
4 4 │
const MyComponent = (props) => { return <div>{props.name}</div>;};code-block.jsx:1:7 lint/nursery/useReactFunctionComponentDefinition FIXABLE ━━━━━━━━━━━━━━━━━━━━━━
ℹ The React component MyComponent is defined as an arrow function.
> 1 │ const MyComponent = (props) => {
│ ^^^^^^^^^^^
2 │ return <div>{props.name}</div>;
3 │ };
ℹ Mixing component definition styles makes component declarations harder to scan.
ℹ Rewrite this component as a function expression or configure `namedComponents` to allow an arrow function.
ℹ 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.
ℹ Unsafe fix: Use a function expression for this component.
1 │ - const·MyComponent·=·(props)·=>·{
1 │ + const·MyComponent·=·function·(props)·{
2 2 │ return <div>{props.name}</div>;
3 3 │ };
const MyComponent = function (props) { return <div>{props.name}</div>;};"arrowFunction"
Section titled “"arrowFunction"”{ "linter": { "rules": { "nursery": { "useReactFunctionComponentDefinition": { "options": { "namedComponents": "arrowFunction" } } } } }}Invalid
Section titled “Invalid”function MyComponent(props) { return <div>{props.name}</div>;}code-block.jsx:1:10 lint/nursery/useReactFunctionComponentDefinition FIXABLE ━━━━━━━━━━━━━━━━━━━━━
ℹ The React component MyComponent is defined as a function declaration.
> 1 │ function MyComponent(props) {
│ ^^^^^^^^^^^
2 │ return <div>{props.name}</div>;
3 │ }
ℹ Mixing component definition styles makes component declarations harder to scan.
ℹ Rewrite this component as an arrow function or configure `namedComponents` to allow a function declaration.
ℹ 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.
ℹ Unsafe fix: Use an arrow function for this component.
1 │ - function·MyComponent(props)·{
1 │ + const·MyComponent·=·(props)·=>·{
2 2 │ return <div>{props.name}</div>;
3 │ - }
3 │ + };
4 4 │
const MyComponent = function (props) { return <div>{props.name}</div>;};code-block.jsx:1:7 lint/nursery/useReactFunctionComponentDefinition FIXABLE ━━━━━━━━━━━━━━━━━━━━━━
ℹ The React component MyComponent is defined as a function expression.
> 1 │ const MyComponent = function (props) {
│ ^^^^^^^^^^^
2 │ return <div>{props.name}</div>;
3 │ };
ℹ Mixing component definition styles makes component declarations harder to scan.
ℹ Rewrite this component as an arrow function or configure `namedComponents` to allow a function expression.
ℹ 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.
ℹ Unsafe fix: Use an arrow function for this component.
1 │ - const·MyComponent·=·function·(props)·{
1 │ + const·MyComponent·=·(props)·=>·{
2 2 │ return <div>{props.name}</div>;
3 3 │ };
const MyComponent = (props) => { return <div>{props.name}</div>;};Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.