EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

React Refs

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » React Native Tutorial » React Refs

React Refs

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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 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 of it.

  • HTML-TAG-NAME: This 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 have 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 because each time we need to create some props and send it 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, its 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>

Popular Course in this category
React JS Redux Training (1 Course, 5 Projects)1 Online Courses | 5 Hands-on Projects | 18+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,487 ratings)
Course Price

View Course

Related Courses

Output:

Before clicking the button,

React Refs-1.1

After clicking the button with text,

React Refs-1.1

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,

Output-2.1

After filling the input box,

Output-2.2

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 –

  1. React Native Authentication
  2. React Native Local Storage
  3. React-Native Calendar
  4. React Router Transition

React JS Redux Training (1 Course, 5 Projects)

1 Online Courses

5 Hands-on Projects

18+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
React Native Tutorial
  • Basic
    • What is React Native?
    • React Versions
    • React Constructor
    • React Native Architecture
    • React Native Libraries
    • React Components Libraries
    • React Native Components
    • React Component Library
    • React Component Lifecycle
    • React Native Framework
    • React Higher Order Component
    • React Ternary Operator
    • React Native Charts
    • React Native Layout
    • React Native Grid
    • React Native Fetch
    • React Native Modal
    • React Native SVG
    • Button in React Native
    • React List Components
    • React Native Element
    • React Native FlatList
    • React Native SectionList
    • react native dropdown
    • React Native Menu
    • React Native State
    • React State Management
    • React Native Tabs
    • React Native Tab Bar
    • React Format
    • React-Native Switch
    • React Native Firebase
    • React Native Flexbox
    • React Native StatusBar
    • React Native ScrollView
    • React Native ListView
    • React Native TextInput
    • React Native Table
    • React-Native Border Style
    • React Native Search Bar
    • React-Native StyleSheet
    • React Native Vector Icons
    • React Native Login Screen
    • React Native Splash Screen
    • React Native Image Picker
    • React Native Navigation
    • React Native Swift
    • React Controlled Input
    • React Fragment
    • React Native Color
    • React Portals
    • React Refs
    • React shouldComponentUpdate()
    • React ComponentDidMount()
    • React componentWillUpdate()
    • React Native Orientation
    • React Native Animation
    • React Native Form
    • React Props
    • React Native Linear Gradient
    • React Native AsyncStorage
    • React Error Boundaries
    • React Native Progress Bar
    • React-Native Calendar
    • React Native Linking
    • React Native DatePicker
    • React Native Image
    • React Native Drawer
    • React Native Drawer Navigation
    • React Native Fonts
    • React Native Overlay
    • React Native OneSignal
    • React Native Template
    • React Native Router
    • React Router Transition
    • React Dispatcher
    • React Native Redux
    • React JSX
    • React native keyboardavoidingview
    • React Native Permissions
    • React Redux Connect
    • React Native Material
    • React Native Gesture Handler
    • React Native Theme
    • React Native Accessibility
    • React Native Justify Content
    • MobX React Native
    • React Native Authentication
    • React Native UUID
    • React Native Geolocation
    • React Native Debugger
    • React Native Carousel
    • React Native Local Storage
    • React Native TypeScript
    • React Bootstrap
    • React Native SQLite
    • React Native Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - React JS Redux Training (1 Course, 5 Projects) Learn More