|
| 1 | +# Transform React Fragment to Shorthand Syntax |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +> [!NOTE] |
| 6 | +> This codemod helps modernize React codebases by using the more concise fragment syntax while maintaining functionality. |
| 7 | +
|
| 8 | +## How the Migration Script Works |
| 9 | + |
| 10 | +The script automates the entire conversion process in a few key steps: |
| 11 | + |
| 12 | +1. **Fragment Detection** |
| 13 | + ```jsx |
| 14 | + // From: |
| 15 | + <Fragment> |
| 16 | + <div>Hello</div> |
| 17 | + <div>World</div> |
| 18 | + </Fragment> |
| 19 | + |
| 20 | + // To: |
| 21 | + <> |
| 22 | + <div>Hello</div> |
| 23 | + <div>World</div> |
| 24 | + </> |
| 25 | + ``` |
| 26 | + |
| 27 | +2. **Import Cleanup** |
| 28 | + ```typescript |
| 29 | + // From: |
| 30 | + import React, { Fragment } from 'react'; |
| 31 | + |
| 32 | + // To: |
| 33 | + import React from 'react'; |
| 34 | + ``` |
| 35 | + |
| 36 | +## Why This Makes Migration Easy |
| 37 | + |
| 38 | +1. **Zero Manual Updates** |
| 39 | + - Codegen SDK handles all Fragment replacements |
| 40 | + - Automatically cleans up imports |
| 41 | + |
| 42 | +2. **Consistent Changes** |
| 43 | + - Ensures all Fragments are converted |
| 44 | + - Maintains code functionality |
| 45 | + |
| 46 | +3. **Safe Transformations** |
| 47 | + - Preserves JSX structure |
| 48 | + - Handles nested Fragments correctly |
| 49 | + |
| 50 | +## Running the Migration |
| 51 | + |
| 52 | + |
| 53 | +The script will: |
| 54 | +1. Find all Fragment components |
| 55 | +2. Convert them to shorthand syntax |
| 56 | +3. Clean up Fragment imports |
| 57 | +4. Preserve other React imports |
| 58 | + |
| 59 | +## Learn More |
| 60 | + |
| 61 | +- [React Fragments](https://react.dev/reference/react/Fragment) |
| 62 | +- [JSX Fragments](https://react.dev/reference/jsx#jsx-fragments) |
| 63 | +- [Codegen Documentation](https://docs.codegen.com) |
| 64 | +- [More on Codegen SDK jsx elements API](https://docs.codegen.com/api-reference/typescript/JSXElement#jsxelement) |
| 65 | +## Contributing |
| 66 | + |
| 67 | +Feel free to submit issues and enhancement requests! |
0 commit comments