React Configuration
Configure React support with JS Style Kit
Overview
JS Style Kit provides comprehensive React support with both ESLint rules and TypeScript integration. This includes support for React hooks, React Compiler, and Next.js projects.
Enabling React Support
React support is disabled by default. To enable it, specify react: true
in your ESLint configuration:
Configuration Options
The React configuration supports several options:
Function Style for Components
The functionStyle
option affects how React components are styled:
Examples of Different Function Styles
Arrow Function Style (default)
Function Declaration Style
Function Expression Style
React Compiler Support
React Compiler is a compiler that optimizes React components for better performance. JS Style Kit enables React Compiler support through the react-hooks/react-compiler
rule, which flags Hooks violations required for the compiler to function.
React Compiler support is enabled by default when React support is enabled. To disable it, use the rules
option:
React Fast Refresh Support
React Fast Refresh is a feature that provides instant feedback on component changes without losing component state. JS Style Kit includes support for validating that your components can safely be updated with Fast Refresh through the eslint-plugin-react-refresh
plugin.
React Fast Refresh validation is disabled by default. It should be enabled for SPAs like Vite. It should be left disabled for Next.js projects. To enable it:
The plugin enforces that your components are structured in a way that integrations like react-refresh
expect, helping you avoid issues with Fast Refresh in development.
What the Plugin Validates
The plugin validates that:
- Components are properly exported (not mixed with non-component exports)
- Anonymous function components are not used
- Export patterns are compatible with Fast Refresh
- Components are not nested inside other components
By default, the configuration allows constant exports alongside components, which is supported by most bundlers including Vite.
Next.js Support
For Next.js projects, enable the next
option:
This will add .next
to the list of ignored directories, preventing ESLint from analyzing the Next.js build output.
TypeScript Integration
When both React and TypeScript are enabled, JS Style Kit automatically configures TypeScript-specific React rules:
React Hooks Rules
JS Style Kit includes the following rules for React hooks:
react-hooks/rules-of-hooks
: Enforces the Rules of Hooksreact-hooks/exhaustive-deps
: Verifies the dependency array of useEffect and similar hooks
Best Practices
The React configuration enforces several best practices:
- Consistent function component definitions
- Proper use of React.Fragment
- Proper use of useState hook
- No array indexes as keys
- Proper use of boolean props
- Self-closing tags for components without children
- And many more
These rules help avoid common pitfalls in React development and ensure consistent code style across your project.