Skip to content

refactor(create-vite): use named imports for react#17773

Merged
bluwy merged 3 commits into
vitejs:mainfrom
Knightwalker:create-vite-template-react
Jul 31, 2024
Merged

refactor(create-vite): use named imports for react#17773
bluwy merged 3 commits into
vitejs:mainfrom
Knightwalker:create-vite-template-react

Conversation

@Knightwalker
Copy link
Copy Markdown
Contributor

Description

This PR refactors the default Vite templates for React, in order to utilize named imports from react-dom/client instead of importing the entire ReactDOM namespace.

The reasons for change were:

  • Clarity: Using named imports makes it explicit which part of the module is being used. This can improve code readability and maintainability.
  • Potential Bundle Size Reduction: If your build system supports tree-shaking (which Vite does), using named imports can potentially reduce the bundle size by excluding unused parts of the module vs bringing in the entire ReactDOM namespace.
  • Consistency: I frequently find myself and my colleagues making this adjustment when starting new projects to ensure we are using the latest React practices.

@bolt-new-by-stackblitz
Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link
Copy Markdown
Member

@ArnaudBarre ArnaudBarre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I agree for using named export.
  • The temporary variable is not needed
  • ! is always better than using as

@nilsingwersen
Copy link
Copy Markdown
Contributor

Same should apply for typescript template for consistency right?

@bluwy
Copy link
Copy Markdown
Member

bluwy commented Jul 30, 2024

FWIW React is still adamant with publishing in CJS, so named imports doesn't help much with treeshaking as CJS is harder to analyze. They also mention that the internals are very intertwined, so even if treeshaking works, you're not reducing by much.

@ArnaudBarre
Copy link
Copy Markdown
Member

Yeah I know that why it was not an issue but I think this is good to follow how every other modules are imported

@bluwy bluwy changed the title refactor(create-vite): update react template refactor(create-vite): use named imports for react Jul 31, 2024
@bluwy bluwy merged commit 6eab91e into vitejs:main Jul 31, 2024
@Knightwalker Knightwalker deleted the create-vite-template-react branch July 31, 2024 06:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants