eslint-plugin-reactfc
v1.0.0
Published
An ESLint plugin to enforce the order of hooks, methods, and rendering logic inside React functional components.
Maintainers
Readme
eslint-plugin-reactfc
An ESLint plugin that enforces the order of hooks, methods, and rendering logic inside React functional components.
What does this plugin do?
This plugin enforces a consistent order for code blocks inside React functional components. It helps maintain readability and best practices by ensuring that hooks, variables, methods, effects, and rendering logic always appear in a predictable order.
Enforced Order
useStatehooks (state management)- Custom hook calls
- Other variables or computed values
- Methods (functions inside the component)
useEffecthooks (side effects)- Conditional rendering logic (if applicable)
- JSX return (rendered output)
Installation
npm install --save-dev eslint-plugin-reactfcUsage
Add the following to your .eslintrc.js or .eslintrc.json:
{
"plugins": ["reactfc"],
"rules": {
"reactfc/order": "warn"
}
}Rule Details
This rule checks the order of code blocks inside React functional components. The expected order is:
useStatehooks- Custom hooks (functions starting with
useexceptuseState/useEffect) - Variables and computed values
- Methods (function declarations or function expressions assigned to variables)
useEffecthooks- Conditional rendering (if statements that return JSX)
- The final
returnstatement with JSX
If any block is out of order, a warning or error will be reported.
Example
Correct:
function MyComponent() {
const [count, setCount] = useState(0); // 1. useState
const data = useMyCustomHook(); // 2. custom hook
const doubled = count * 2; // 3. computed value
const handleClick = () => {}; // 4. method
useEffect(() => {}, [count]); // 5. useEffect
if (!data) return null; // 6. conditional rendering
return <div>{doubled}</div>; // 7. JSX return
}Incorrect:
function MyComponent() {
const data = useMyCustomHook();
const [count, setCount] = useState(0); // useState should come first
const doubled = count * 2;
useEffect(() => {}, [count]);
const handleClick = () => {}; // method should come before useEffect
if (!data) return null;
return <div>{doubled}</div>;
}Development & Testing
- All rules are in
index.js. - Tests are in
order-rule.test.jsand use Jest and ESLint's RuleTester. - To run tests:
npm testContributing
Feel free to open issues or pull requests for bug fixes, new features, or improvements.
License
MIT
