Refs are a function provided by React to access the DOM element and the React element that you might have created on your own. They are used in cases where we want to change the value of a child component, without making use of props and all. They also provide us with good functionality as we can use callbacks with them.
In the above example, we are making use of the target value of event e, for getting the value of what Mukul is saying. This above output can also be achieved by using the refs.
In the above we make use of refs provided by React, they can also be used to add callback functions inside them which is helpful in many cases.
When to use refs
- Helpful when using third party libraries.
- Helpful in animations.
When not to use refs
- Should not be used with functional components because they dont have instances.
- Not to be used on things that can be done declaritvely.
- ReactJS | Introduction to JSX
- ReactJS | ReactDOM
- ReactJS | Rendering Elements
- ReactJS | Components
- ReactJS | Components - Set 2
- ReactJS | State in React
- ReactJS | Lifecycle of Components
- ReactJS | Props - Set 1
- ReactJS | Implementing State & Lifecycle
- ReactJS | Props - Set 2
- ReactJS | PropTypes
- ReactJS | Importing and Exporting
- ReactJS | Calculator App ( Styling )
- ReactJS | Setting up Development Environment
- ReactJS | Conditional Rendering
- ReactJS | Lists
- ReactJS | Keys
- ReactJS | Transition Group
- How to create a Color-Box App using ReactJS?
- ReactJS | Introduction to Babel
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ManasChhabra2