react-file-formatter
is a command-line tool designed to help React developers standardize and clean up their React code files for improved readability and maintainability. The tool automatically formats your React code and allows you to reorder functions and hooks based on a custom order specified by the user.
If you find any issues kindly reachout! or create a issue in github repo.
You don’t need to install this package globally. You can use it directly with npx
from the command line:
npx react-file-formatter --format-all
npm i react-file-formatter -g
In React projects, it's common to use multiple hooks and functions in a single file. Over time, this can lead to disorganized and hard-to-read code. react-file-formatter solves this by automatically:
-
The tool automatically sorts hooks, functions, and return statements based on a custom order, making the code more consistent and easier to understand.
-
In addition to reordering, the tool also formats the code using Prettier, ensuring consistent formatting across your project. This eliminates the need to worry about inconsistent indentation, quotes, and other formatting issues.
- Customizable Ordering: Define your preferred order of hooks and functions in a simple JSON file.
- Prettier Integration: Automatically format your code according to your Prettier configuration.
- CLI Tool: Easily use the tool from the command line to format your files.
Install globally or project specify or just run the command
npx react-file-formatter --format-all
npm install -g react-file-formatter
npm install react-file-formatter
.reactfileformaterrc.json file will be created automatically in the root of your project. Where you can modify and configure code based on your project needs. NOTE: content property is important to format the code files. Make sure it exists!
{
"content": ["./src", "./utils"],
"order": [
"useContext",
"useState",
"useRef",
"useMutation",
"function",
"ArrowFunctionExpression",
"useEffect",
"return"
],
"prettier": {
"trailingComma": "es5",
"tabWidth": 2,
"singleQuote": true,
"semi": true
}
}
npx react-file-formatter <file-path>
npx react-file-formatter <file-path> <custom.json-path>
react-file-formatter <path-to-file>
-
If you have a file, e.g., src/components/Button.js, and you want to format it:
-
This will format the file Button.js and clean up the code according to the formatting rules defined in the module.
npx react-file-formatter --format-all
This command will check the .reactfileformaterrc.json
configuration file and excute the formatting process only to
the folder paths which is mentioned in the content
property.
Command | Flags |
---|---|
npx react-file-formatter | --format-all |
npx react-file-formatter | --format-jsx |
npx react-file-formatter | --format-tsx |
npx react-file-formatter | --format-js |
npx react-file-formatter | --format-ts |
If it is a react component it will apply reordering else it will apply the prettier rules which is specified.
- JSX, TSX files
- React custom hook/hook {ts,js} files
- .js, .ts
import { useState, useEffect, useContext, useRef } from "react";
const MyComponent = () => {
const [state, setState] = useState(0);
const inputRef = useRef(null);
const context = useContext(MyContext);
useEffect(() => {
console.log(state);
}, [state]);
const handleClick = () => {
setState(state + 1);
};
return <div onClick={handleClick}>State: {state}</div>;
};
import { useContext, useState, useRef, useEffect } from "react";
const MyComponent = () => {
const context = useContext(MyContext);
const [state, setState] = useState(0);
const inputRef = useRef(null);
useEffect(() => {
console.log(state);
}, [state]);
const handleClick = () => {
setState(state + 1);
};
return <div onClick={handleClick}>State: {state}</div>;
};
Feel free to contribute to the project! To get started, fork the repository, make your changes, and create a pull request.
This project is licensed under the MIT License.