React Formatter for JSX and TSX
WM-78Paste a React component to format it with Prettier. Use Format JSX for JavaScript or Format TSX for TypeScript. Choose indent, quotes, semicolons and line width.
Runs in your browser. Nothing is uploaded.
React component (JSX or TSX)
Formatted code
How to use it
- Paste your React component into the input box.
- Pick Format JSX for JavaScript files, or Format TSX for TypeScript files.
- Set the indent, line width, quotes and semicolons.
- Copy the formatted component or download it as a .jsx or .tsx file.
JSX and TSX
Format JSX uses Prettier with the Babel parser, which reads JavaScript with JSX. Format TSX uses the TypeScript parser, so type annotations, interfaces, generics and props types are accepted. Pick the action that matches your file. Type annotations fail in Format JSX, and the two parsers also read a few constructs differently, such as an arrow function with a generic type parameter.
What changes in a component
Only layout changes. Long JSX is wrapped in parentheses and attributes are put one per line when they do not fit. Props, hooks and logic are not touched. Imports are not reordered or removed, and the code is not run, linted or type checked. Quote style is set separately for JavaScript strings and for JSX attributes, and semicolons can be switched off.
Errors
If the code does not parse, the error names the line and column where the parser stopped and nothing is changed. The position can be just after the real mistake, such as a missing closing tag at the end of a component. If the error mentions a type or an unexpected colon, you probably need the TSX action.
Error messages and what to do
| Message | What it means | Fix |
|---|---|---|
Invalid TSX: Type expected. at line 1, column 10 | The TypeScript parser expected a type after a colon and found none. | Add the type, for example const a: number = 1. |
Invalid JSX: Unexpected token at line 1, column 8 | The JavaScript parser found something it cannot read, such as a TypeScript type annotation. | Check the syntax at the position, or use Format TSX for TypeScript code. |
Questions people ask
How do I format a React component?
Paste the component and click Format JSX, or Format TSX if it is TypeScript. Prettier rewrites the indentation, line breaks and quotes. Copy the result or download it. Props and logic are not changed.
What is the difference between Format JSX and Format TSX?
Format JSX reads JavaScript with JSX using the Babel parser. Format TSX reads TypeScript with JSX using the TypeScript parser, so types and interfaces work. Use the one that matches your file extension, .jsx or .tsx.
Does it work with hooks and function components?
Yes. Hooks, function components, class components and arrow functions are all ordinary JavaScript or TypeScript with JSX. The formatter only changes layout, so useState and useEffect calls keep working exactly as before.
Does it sort or remove my imports?
No. Import lines are kept in the order you wrote them, and unused imports stay. The formatter changes spacing and line breaks only. Use an editor setting or a linter for import sorting and cleanup.
Can I format without semicolons?
Yes. Untick Semicolons and Prettier prints the code without them, adding a leading semicolon only on lines where the code would otherwise be read wrongly. The behavior of the code does not change.
How do I match my project's Prettier settings?
Set the indent, line width, quote style and semicolons here to the values in your project's Prettier config. Options such as trailing commas and bracket spacing stay at Prettier's defaults on this page, so a few lines may still differ.
About this tool
Format JSX uses Prettier with the Babel parser for JavaScript with JSX; Format TSX uses the TypeScript parser, so type annotations, interfaces and generics are accepted. Both change layout only: indentation, quotes, semicolons and how long JSX is wrapped. Hooks, props and logic are not touched, and the code is not run, linted or type checked. Pick the action that matches your file, because JSX with type annotations fails in Format JSX and a few JSX-only constructs read differently in TSX. A syntax error is reported with its line and column and nothing is changed. Comments are kept.