Updated April 15, 2023
Definition of React Refs
React js is a way to get the reference of the html elements; for example, if we have many elements and we want to perform some changes on that element, then we can create a refs tag, and in that tag, we can do some changes. most of the time, it is avoided to use the ref tag in the react js because it searches for the ref tag with that name. there is the only way to modify the elements of any child components in the react js is by sending props to the child components from the parent components, but with the help of the ref, we can do it without sending props from parents to the child.
Syntax:
Below is a simple syntax for the ref in the react js. here from the below syntax, we can see that there used to two important components of it one is the HTML element like div, and the other is ref tag which can be given any names. We can explain the attributes of it with the help of some of the important points.
- HTML-TAG-NAME: This is the name of html attribute like div, input, etc., and once we write the ref inside the name of the tag, when we will try to do some or any operations over them, they will search with the name of the html tag which we have mentioned.
- DETAILS-OF-REF: Here, we can assign some value that we can access and modify.
- ref: This is the key used in the HTML as the one element of any HTML tag, as we can create just like we used to create the names of it and class in the html.
Please see the below syntax for a better understanding.
<HTML-TAG-NAME ref = "DETAILS-OF-REF"></input>
How do Refs Work in React?
Before working and understanding the concept of the refs in the react js, we need to understand why we need the ref in react js. You had seen that when you needed to do any change in the child components from the parent components, then we needed to pass the props from the parent components to the child components and according to the value of the props, the child components get reflect the change which we wanted. Now, this approach may be longer if we wanted to do change for a very unique and small one because each time, we need to create some props and send them to child components to perform the change. Now with the help of the ref tag, we can assign the name to the ref, and at any moment in any component, we can do or perform some changes with the name of the ref which we have given to it .let’s take an example if we have given name to a ref as x and we want to do some modification to the x ref then to perform the modification it will search for the whole HTML nodes which are costly operations, so we can use it when it is needed. We can give some points which will explain more working of the ref in the react.
- Refs are not any special package in the react js; it’s a newly added attribute to any html.
- When we use the ref, then it allows us to find the attribute with the name which we have given to it, and we do some modifications, or simply we can get the value of that ref without using props.
Examples of React Refs
Below we have given two examples for the react ref where in one case, we are capturing the input on clicking the button and displaying the data on other html attributes and in the other case, we are capturing the input text on writing and displaying it on other html attributes. We can run the example we need to create two files one is with .js extension and another with .html extension and paste the js and html codes on them, and we get the out.
Example #1
In the below examples, we have taken an input box, and we have given a ref value to the input box, and there is a submit button. Whatever we will write inside the input box after clicking the submit button, the input field will be captured and displayed on the other h2 element of the html.
Code:
Javascriptparts ,
class Example extends React.Component {
constructor(props) {
super(props)
this.textAttribute = React.createRef();
this.state = {
textValue: ''
}
}
onSubmitHandler = element => {
element.preventDefault();
let val =this.textAttribute.current.value
this.setState({ textValue: val})
};
render() {
return (
<div>
<h2>This is an example of react ref</h2>
<h2>The Input values: {this.state.textValue}</h2>
<form onSubmit={this.onSubmitHandler}>
<input type="text" ref={this.textAttribute} />
<button>Add Element</button>
</form>
</div>
);
}
}
ReactDOM.render(<Example />, document.getElementById("main"));
HTML parts ,
<div id="main"></div>
Output:
Before clicking the button,
After clicking the button with text,
Example #2
In the below example, we have used ref to display the input which we are writing inside the html field will be captured with the help of the ref, and the same text will be displayed on another html element. Hence we are able to capture the elements and able to perform the operations over the attributes without using state and props.
Code:
Javascript parts,
class Example extends React.Component{
constructor(props){
super(props);
this.state={TextInput:null}
}
componentDidMount(){
this.data.oninput=this.changeText.bind(this);
}
changeText(e){
this.setState({TextInput:e.target.value})
}
render(){
return(
<div>
<input ref={(val)=>this.data=val} type="text"/>
{this.state.TextInput}
</div>
)
}
}
ReactDOM.render(<Example/>,document.getElementById("main"));
Html section ,
<div id="main">
</div>
Output:
Before filling the input box,
After filling the input box,
Recommended Articles
This is a guide to React Refs. Here we also discuss the definition and working of react refs along with different examples and its code implementation. You may also have a look at the following articles to learn more –