--- title: sort-comp description: sort-comp rule tags: ['quality', 'react'] category: quality autofix: suggestions --- sort-comp rule # sort-comp > **Keywords:** React, component methods, ordering, lifecycle, code organization, ESLint rule, LLM-optimized Enforce component method ordering in React class components. This rule is part of [`eslint-plugin-react-features`](https://www.npmjs.com/package/eslint-plugin-react-features). ## Quick Summary | Aspect | Details | | -------------- | -------------------------------------------------------------------- | | **Severity** | Warning (organization) | | **Auto-Fix** | ❌ No (requires reordering) | | **Category** | React | | **ESLint MCP** | ✅ Optimized for ESLint MCP integration | | **Best For** | React class components with consistent style | ## Rule Details Enforces a consistent ordering of methods within React class components, making code more predictable and easier to navigate. ### Why This Matters | Issue | Impact | Solution | | ----------------------------- | ----------------------------------- | ---------------------------- | | 🔄 **Inconsistent structure** | Hard to find methods | Standardized ordering | | 🐛 **Review difficulty** | Different patterns across team | Consistent conventions | | 🔍 **Onboarding** | New devs struggle to navigate | Predictable structure | ## Default Order 1. `static-variables` - Static class properties 2. `static-methods` - Static methods 3. `instance-variables` - Instance properties 4. `lifecycle` - React lifecycle methods 5. `everything-else` - Custom methods 6. `render` - The render method (always last) The `lifecycle` group holds the constructor and React's lifecycle methods (`componentDidMount`, `shouldComponentUpdate`, `componentDidUpdate`, `componentWillUnmount`, …); their order among themselves is not checked. Member names are read in every spelling (`['render']()`). The class must extend React's `Component` or `PureComponent`, by binding, declared or as a class expression. ## Examples ### ❌ Incorrect ```tsx class UserProfile extends React.Component { // BAD: render before lifecycle render() { return
{this.state.name}
; } // BAD: lifecycle after render componentDidMount() { this.fetchUser(); } // BAD: constructor not first constructor(props) { super(props); this.state = { name: '' }; } // BAD: static after instance methods static propTypes = { userId: PropTypes.string.isRequired }; } ``` ### ✅ Correct ```tsx class UserProfile extends React.Component { static propTypes = { userId: PropTypes.string.isRequired, }; state = { name: '' }; constructor(props) { super(props); } componentDidMount() { this.fetchUser(); } fetchUser() {} render() { return
{this.state.name}
; } } ``` ## Options | Option | Type | Default | Description | | -------- | -------- | ---------------- | --------------------------------- | | `order` | string[] | See default order| Array of group names in order | ### Configuration with Options ```javascript { rules: { 'react-features/sort-comp': ['warn', { order: [ 'static-variables', 'static-methods', 'lifecycle', 'instance-variables', 'everything-else', 'render' ] }] } } ``` ## Configuration Examples ### Basic Usage ```javascript { rules: { 'react-features/sort-comp': 'warn' } } ``` ## Related Rules - [`prefer-stateless-function`](./prefer-stateless-function.md) - Functional components - [`state-in-constructor`](./state-in-constructor.md) - State initialization ## Further Reading - **[Component Lifecycle](https://react.dev/reference/react/Component)** - React docs - **[Class Components](https://react.dev/reference/react/Component#defining-a-class-component)** - Structure ## Known False Negatives - Lifecycle methods are not ordered among themselves; only the groups are. - `getDerivedStateFromProps` is a static method, so it sorts with `static-methods`.