Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jul 31, 2024

Conversation

Knightwalker
Copy link
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.

Copy link

stackblitz bot commented Jul 27, 2024

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

Copy link
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

Same should apply for typescript template for consistency right?

@bluwy
Copy link
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
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
12 checks passed
@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