DEV Community

Cover image for šŸ”„ Learn React in 10 Tweets (with hooks)

šŸ”„ Learn React in 10 Tweets (with hooks)

Chris Achard on September 20, 2019

This was originally posted as a twitter thread: https://twitter.com/chrisachard/status/1175022111758442497 šŸ”„ Learn React in 10 tweets (with hooks)...
Collapse
 
dzhavat profile image
Dzhavat Ushev

I haven't used React in the last three years but these steps were quite clear and helped me understand how it has evolved. Thanks :)

Collapse
 
karataev profile image
Eugene Karataev

I see only one change of modern React compared to React 3 years ago: hooks instead of class components with local state and lifecycle methods. I guess in the next three years React will not change dramatically as well.

Collapse
 
chrisachard profile image
Chris Achard

I agree.

Also - classes still work great, and there are no plans to deprecate them! I only choose hooks for this guide because I find them easier to explain than classes. I hope to do another guide in the future though (a part 2 I guess) that explains classes - because there are SO MANY classes in the wild that React developers will definitely encounter them.

Thread Thread
 
karataev profile image
Eugene Karataev

Btw, I still prefer to use class components instead of hooks, because I don't want to mix two approaches in one codebase (even if it's the acceptable strategy as described in the official docs).

Thread Thread
 
seanmclem profile image
Seanmclem

I see increasingly hooks described as an alternative to classes. But hooks are really just something you can use in functional components. Functional components are the alternative to classes and don't require hooks

Thread Thread
 
chrisachard profile image
Chris Achard

Correct, yes. Hooks are what enables function components to replace classes though- otherwise you can have local state. You could do everything in redux though say, and you wouldnā€™t need ā€˜useStateā€™

Thread Thread
 
seanmclem profile image
Seanmclem

Hooks help functions replace classes only when those class components used state or life cycle. Otherwise it could have used a functional component all along and that's always been the alternative to classes. I'm not sure what my point was anymore but it's fun to discuss

Thread Thread
 
chrisachard profile image
Chris Achard

:) Yes - you're correct; the main use case for hooks is state or lifecycle (useEffect). You can also use useRef for reference though, useContext for context, and a few other things - but useState is probably most of the usage.

Collapse
 
tobi4161 profile image
Tobias Dixen

I don't think you can update state as you do in 7. If you set state derived from the current state, you should pass in a function which takes the previous state as parameter and returns the new state:

setCount(prevCount => prevCount + 1)
Collapse
 
chrisachard profile image
Chris Achard

That's important if you're doing multiple setCount calls or async ones, yes absolutely. With just the one, it's fine to use the previous value.

But yes! That's a good point for the next course :) Thanks

Collapse
 
francosirena profile image
Franco Sirena

Hey Chris in order to keep your teachings high quality I would strongly suggest for you to change the number 7.
Clicking thrice fast enough in the button can already cause an inconsistency between clicks and count value.
I have to remember people all the time that setState is an async action, and we often find flaky tests because of patterns like the one you're using, so, please, for the sake of newcomers doing the right thing. Change it to use the callback syntax.

Thread Thread
 
chrisachard profile image
Chris Achard

I'll have to test that, yeah - I often forget about the callback syntax myself :/ so it would be good for me to remember too! Thanks for the tip

Collapse
 
chrisachard profile image
Chris Achard

šŸ¤£ - My goal is to get people "over the first hurdle" - and really, the core of React isn't that big!

There is obviously more you need to know to write a full app, but I tried to fit in the base of what I use everyday. Hope it helps!

Collapse
 
_martinjose28_ profile image
martin jose

This post is amazing. I've been learning react this week, I am feel so comfortable creating a simple UI, I already added authentication using okta, and make http request to an API that I build about 2 months ago (mongo, node, express).

I have to say I prefer classes instead of hooks. Idk it seems more complicated I know that things become easier when we try. But for now I will keep using classes if I need to work with state. (BTW in this post finally I understand how to work with hooks ) thanks (y)

Collapse
 
chrisachard profile image
Chris Achard

That's awesome! Glad it helped

Yeah - classes are totally fine! Both classes and hooks are fully supported - so if you like classes better, then go for it :)

Collapse
 
nibble profile image
Joseph Mawa • Edited

Concise explanation. Simple to grasp. Thanks a bunch for the article. One quick question though.
Why does esLint give a warning whenever i use a variable defined outside, inside the effect without passing it as a dependency. Like:

   useEffect( () => {
       fetch(url)
      .then(res => res.json())
      .then( data => console.log(data))
}, [])

url is defined outside and obviously i want to fetch the data once after initial render but esLint gives a warning requiring me to pass it as a dependency. Does it mean i can't use a variable defined outside the effect if i don't pass it as a dependency?

Collapse
 
reneschwarz222 profile image
ReneSchwarz222

I have just seen your lessons and reactscreencasts.
Really, I have learned about react. Thank you very much.
Certainly, you are very excellent developer.
I'd like you to explain to me about the difference between react and react native.
I hope you to reply.

Collapse
 
surajkamdi profile image
Suraj Kamdi

Nice post.. šŸ‘šŸ»

Collapse
 
sonicoder profile image
GĆ”bor SoĆ³s

It is awesome how short and definite it is. Good job!

It has made me think about creating it for Vue and Angular šŸš€

Collapse
 
fredkodye profile image
fredkodye

This was very helpful.

Thanks for your contribution @chrisachard .

Collapse
 
chrisachard profile image
Chris Achard

Glad it helped!