Recommended way of importing React #518
filiptammergard
started this conversation in
General
Replies: 1 comment 4 replies
-
I'd keep it simple and do whatever https://beta.reactjs.org/ does. Ideally we'd archive this cheatsheet and just link to the relevant sections on reactjs.org but it will take a while to upstream everything. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The first opinionated statement in the Basic Cheatsheet is that the most future proof way of importing React is using namespace import:
This might have been true a few years ago, but I think the widely spread recommended way nowadays is using destructured named imports, and only import what you need explicitly:
React Beta docs uses detructured named imports and I don't see why using TypeScript would make it different.
The announcement of the new JSX Transform that came with React 17 made it clear that destructured named imports is the preferred style going forward. Here's a quote of what the provided codemod does:
Along where we say using namespace imports is the most future proof way in the Basic Cheatsheet, we're also saying this:
Since default imports are not recommended, I'm not sure we even need to keep this at all. For many React developers, destructured named imports is becoming the new most familiar way of importing React. If not throwing it out completely, I think it should be reworked a bit and placed in
<details>
.I'm happy to work on this according to my thoughts here, but I'd like to hear your thoughts as well first since I'm not sure if this is controversial. I'd also like to hear your opinion on direction. Should we:
allowSyntheticDefaultImports
quirk needed to get default import of React working, there's not really anything special about importing React when using TypeScript compared to JavaScript. Why not let React's best practices apply and add something here if there is something TypeScript related to keep in mind in the area?Let me know what you think. Particularly interested in your thoughts @eps1lon!
Beta Was this translation helpful? Give feedback.
All reactions