This example demonstrates how to use Codegen to automatically convert React Fragment components to the shorthand syntax (<>). The script makes this process simple by handling all the tedious manual updates automatically.
Note
This codemod helps modernize React codebases by using the more concise fragment syntax while maintaining functionality.
The script automates the entire conversion process in a few key steps:
-
Fragment Detection
// From: <Fragment> <div>Hello</div> <div>World</div> </Fragment> // To: <> <div>Hello</div> <div>World</div> </>
-
Import Cleanup
// From: import React, { Fragment } from 'react'; // To: import React from 'react';
-
Zero Manual Updates
- Codegen SDK handles all Fragment replacements
- Automatically cleans up imports
-
Consistent Changes
- Ensures all Fragments are converted
- Maintains code functionality
-
Safe Transformations
- Preserves JSX structure
- Handles nested Fragments correctly
The script will:
- Find all Fragment components
- Convert them to shorthand syntax
- Clean up Fragment imports
- Preserve other React imports
Feel free to submit issues and enhancement requests!