Hey React engineers! In this article, I'll explain the 4 most important Hooks you need to know in React. Don't worry, I'll not write a long essay a...
For further actions, you may consider blocking this person and/or reporting abuse
Can you clarify the ref hook? From your example I am confused, does the ref help us to make an instance to this same component ( something like new keyword )? Or the ref just give us the access to the current keyword (currently visible DOM element) and nothing more?
Other than that, very good explanation, thank you!
Short answer: useRef refers to an object and the .current property allows you to access its instance. It is what you do with useRef that matters.
The core usage of useRef hook is to store reference of element.
What's happening in this code snippet above is that we've got a text input and right below that a button. We know that the text input will get focused when clicked but that is not the concern here. The challenge is to focus the text input when the button (another element) is clicked. How?
There are just 3 easy steps you need to follow. First, call useRef and get the value, in this case "theInput". Then we need to connect that value to any element, in this case the input element by writing (ref={theInput}). At this point, "theInput" and element in connected, or in other words, "theInput" now refers the input element. Finally, we can change the state of the input element to be focused, because we've got a reference to it using theInput.current.focus().
I'm sorry but that's bollocks.
useRef
has nothing to do with the DOM. It's stated clearly on the offical docs:Please don't spread false information.
useRef
is useful whenever you want to reference the same value on every render, but not trigger a re-render when its value changes.That's it. Thank you 👍
@mrtnbroder apparently you're right, my bad. I just need to remove the "DOM" part in that comment, will edit it now. That's how I always understood the useRef hook. Thanks dude.
Thank you very much for the clarification! So in other words useRef is like querrySelector function in the pure JS way. Please correct me if I am wrong.
You're right. Not to worry, usually you don't need it.
yes, the difference is querySelector is imperative, useRef declarative, also querySelector does a search for the string you pass to it, so you have to know that is unique and won't change.
useRef on the other hand, ties itself to that jsx element and nothing else.
Cool!
Nice write up! To clarify, the only way to trigger a render is to use the state setting function (ie
setState()
). If you update the state variable any other way, it not only won't trigger a rerender, but if something ELSE triggers that rerender, if your variable is a primitive like boolean, number or string, you'll lose your changes with the rerender!💜Yes, totally forgot that. Thanks for reminding me. I'll edit the article. 😄
I would suggest to add Case 3 for useEffect() hook mentioning clean-up function (the return function). Use case example: When you need to refresh a particular component let's say every 5 minutes then you would set the interval in useEffect as usual but to clear the interval which should be done in this clean-up return function (similar to componentWillUnmount class component lifecycle method). I'd recommend a short article on this, by Martín Mato - dev.to/otamnitram/react-useeffect-...
Overall, nice and concise article. Thanks for this quick read!
Thanks bro, I'll add it to my article.
You could simplify
return <>{isVisible && <h1>I'm visible</h1>}</>;
to
return isVisible && <h1>I'm visible</h1>;
and I think this is a syntax error:
<ScrollView onContentSizeChange={() => }>
Yes could've simplified that one. As for the syntax error, it's not a syntax error, but just the opening tag of ScrollView component in React Native. I wrote "//..." below that line to indicate that more content is expected in the code snippet but that content doesn't matter for the tutorial. I hope we're friends now hehehehh.
Nice.. can you do one on creating custom hooks 🪝
Bro, I wanted to write an article on this, but I stumbled upon a pretty good article on how to create custom hooks, here you go: dev.to/keyurparalkar/creating-cust...
I've made a bunch of custom hooks that work well without overcomplicating stuffs. Next post will be about that. Follow me so that you don't miss that post.
Interesting article
Thank you.
Too short unfortunately, useless code snippets.
Cool.
Always nice to read so much information laid out clear and concise.
Thank you.
What do you think of Hook useReducer?
I've never used it. 😄
nice post, man! Thank you! :)
Thanks man. 😃
Nice article, simple and precise. thanks
Glad you liked it.
Nice explaination of hooks 👍👍
Thanks dude!
Great this is very helpful
Thank you, give it a like!
Very concise & helpful to read, thanks for the great post, looking foward to see your article about why usecontext + usereducer are not a subtitute for redux.
Thanks man. Appreciate that. 😄