useRef returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). The returned object will persist for the full lifetime of the component. source
The ref object is extremely powerful as it stores a mutable object that changes and does not trigger a component render. This can help our component performance!
Before hooks we had createRef which has the same functionality as useRef but for class components.
Let's have a look at a real-world situation.
Imagine you need to design a custom upload button and the default input type file doesn't meet all your requirements but it does have the file system dialog and extension configuration.
We want to use a material-ui icon button for uploading a file and we are going to use useRef to do that.
As you can see in our MyCustomUploadButton component, we defined material icon button that triggers input file click on click. the rest of the functionality is handled by the input file onChange. The input itself is never displayed on the screen.
This example above is 1 of many use cases where useRef can come in handy!.