- Notifications
You must be signed in to change notification settings - Fork4.2k
Recommended way of importing React#518
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
The first opinionated statement in the Basic Cheatsheet is thatthe most future proof way of importing React is using namespace import: import*asReactfrom"react"; 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: import{useState} from"react"; 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 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:
Let me know what you think. Particularly interested in your thoughts@eps1lon! |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 2 comments 5 replies
-
I'd keep it simple and do whateverhttps://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. |
BetaWas this translation helpful?Give feedback.
All reactions
-
Does that mean you'd go for removing the section about importing React in the cheatsheet in this case? |
BetaWas this translation helpful?Give feedback.
All reactions
-
could you link that section? just to make sure there's nothing we should keep. But I wouldn't recommend an import style here. That's up to the library. |
BetaWas this translation helpful?Give feedback.
All reactions
-
Ah sorry, here it is:https://react-typescript-cheatsheet.netlify.app/docs/basic/setup#import-react |
BetaWas this translation helpful?Give feedback.
All reactions
-
Yeah that section can be removed. It's really annoying if that comes up in a discussion and one party links the official docs and the other party links this cheatsheet. We should just sync with the official docs. And if they still use |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
1.When should you use TypeScript with React instead of just JavaScript?
3.What is the difference between interface and type in TypeScript? Which one to use for props?
4.How to define a functional component with TypeScript?
6.How to style events in handlers like onClick, onChange, onSubmit...?
7.How to use TypeScript utility types like Partial, Pick, Record, Omit in React? |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
thanks, helpful |
BetaWas this translation helpful?Give feedback.