r/javascript • u/keraito • Mar 17 '22
Why Did And Don’t You Need To Import React
https://www.chakshunyu.com/blog/why-did-and-dont-you-need-to-import-react/?utm_source=reddit&utm_medium=social&utm_campaign=r_javascript-6
Mar 18 '22 edited Mar 18 '22
Because the old way to use React uses states, which are class based. You can still do this if you wish. The new (recommended) way to use React is functional.
States (and classes as you know) use dot notation. Functional programming of course uses functions and does not.
Old react: React.useRef()
New React: useRef()
This is why you need to import React. So you can call its methods.
At least that’s my understanding.
Edit: I like the title btw
1
u/reohh Mar 20 '22
Lmao wat
-1
Mar 20 '22
Oh well. Guess I guessed wrong then, or maybe I explained terribly. It's just a lot of old state based react stuff (which I never use) I see this pattern. React.useRef() etc. So I assumed it was something to do with useRef being called as a method on the imported React object. Whereas 'new React' is functional, so there are not methods on classes, only functions and no need to import React itself. But obviously the answer is something else.
Maybe someone could explain the correct answer then for the OP.
0
u/reohh Mar 20 '22
Dude are you dense?
OP linked an article that explained it, he didn’t ask a question. And you sound like someone who has never used React in their life.
1
Mar 22 '22
Lol. I guess I was half asleep and trying to be helpful. Never mind.
And no I'm not dense, far from it. Just wasn't paying attention. What a crime. I should hang my head in shame.
54
u/Modular_Mindy Mar 17 '22
The title of this article hurts me in ways I did not know words could hurt me.