DEV Community

Gian Franco De La Cruz Mendoza
Gian Franco De La Cruz Mendoza

Posted on

React.js Hook questions

Hello, Dev Community.
I am a new developer who wants to learn React.js.
I want to know about useContext, and useRef.
How & When can I use this hooks in React?

Top comments (1)

Collapse
 
lexlohr profile image
Alex Lohr

useContext is a way to transfer state data from somewhere up in the component tree to any component inside it; its use is to avoid prop drilling, which means that you carry the state as props through every component from the parent to the grand-grand-grand-child that requires it.

useRef is basically a useState without the setter; you get a memoized object. While you usually only use its current-property, you could do whatever you like with it (but in this case, be sure to document it). You can use it to create a ref used by the component to access the rendered DOM node, or a way to carry state without the reconciler ever noticing a difference.