SlideShare a Scribd company logo
React with Ref
In this article, We will discuss the manipulation of DOM elements with
Ref directly with React.
React Framework builds your components and abstracts your code away
from manipulation within the DOM but still leaves the door open for
developers to access it. Reason are few cases where it might be
necessary. That's why React provides an escape hatch know as refs.
Refs are a function that use to access the DOM from components. You
only need to attach a ref to the element in your application to provide
access to it from anywhere within your component without making use
of props and all.
We can also use Refs to direct access to React elements and use
callbacks with them.
We should only use refs when the required interaction cannot be
achieved using state and props.
Use Refs
We can use refs to do anything that needs to be manipulated in the
DOM. Some good cases like focus, test selection, media playback,
triggering mandatory animations, or integration with the third-party
DOM library.
Note: We should avoid using refs because it removes the purpose of
using React. For example, If you want to show and hide a popup
component. We should use a boolean prop for it instead of manipulating
dom.
Creating Refs
We can use React.createRef()method to create Refs, and then we
can attach to a Dom element via the ref attribute after that, we can
access and modify that element through the ref.
class App extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
render() {
return <div ref={this.myRef} />;
}
}
In above code, We created this.myRef in the constructor by calling
React.createRef() method.
Then in the render method , we attached the returned value to ref of
the div element, a reference to the node becomes accessible at the
current attribute of the ref.
We should not use ref attribute on function components because they
do not have instances.
React will assign the current property with Dom element when
component mount and assign null to it when component unmount.
ref updates happen before componentDidMount or
componentDidUpdate methods.
We can pass refs as props to the component. Example:
function MyCustomTextInput ({ myInputRef }) {
return <input ref={myInputRef} />;
}
class App extends React.Component {
constructor(props) {
super(props);
this.myInputRef = React.createRef();
}
componentDidMount() {
this.myInputRef.current.focus();
}
render() {
return <MyCustomTextInput inputRef={this.myInputRef} />;
}
}
In above code, App passed its ref as props to MyCustomTextInput
component.
You can read more in detail about call back ref in this tutorial about
React with Ref.

More Related Content

What's hot

Angular 2 - An Introduction
Angular 2 - An IntroductionAngular 2 - An Introduction
Angular 2 - An Introduction
NexThoughts Technologies
 
Get rid of controllers in angular 1.5.x start using component directives
Get rid of controllers in angular 1.5.x start using component directivesGet rid of controllers in angular 1.5.x start using component directives
Get rid of controllers in angular 1.5.x start using component directives
Marios Fakiolas
 
Web components are the future of the web - Take advantage of new web technolo...
Web components are the future of the web - Take advantage of new web technolo...Web components are the future of the web - Take advantage of new web technolo...
Web components are the future of the web - Take advantage of new web technolo...
Marios Fakiolas
 
What is Angular version 4?
What is Angular version 4?What is Angular version 4?
What is Angular version 4?
Troy Miles
 
What are the components in React?
What are the components in React?What are the components in React?
What are the components in React?
BOSC Tech Labs
 
Angular 4 - quick view
Angular 4 - quick viewAngular 4 - quick view
Angular 4 - quick view
Michael Haberman
 
Angular 5
Angular 5Angular 5
Angular 4 and TypeScript
Angular 4 and TypeScriptAngular 4 and TypeScript
Angular 4 and TypeScript
Ahmed El-Kady
 
Routing & Navigating Pages in Angular 2
Routing & Navigating Pages in Angular 2Routing & Navigating Pages in Angular 2
Routing & Navigating Pages in Angular 2
Knoldus Inc.
 
Developing a Demo Application with Angular 4 - J2I
Developing a Demo Application with Angular 4 - J2IDeveloping a Demo Application with Angular 4 - J2I
Developing a Demo Application with Angular 4 - J2I
Nader Debbabi
 
Powershell of component object model COM Hijacking
Powershell of component object model COM Hijacking Powershell of component object model COM Hijacking
Powershell of component object model COM Hijacking
harr0ey
 
Understand components in Angular 2
Understand components in Angular 2Understand components in Angular 2
Understand components in Angular 2
codeandyou forums
 
Dive into Angular, part 3: Performance
Dive into Angular, part 3: PerformanceDive into Angular, part 3: Performance
Dive into Angular, part 3: Performance
Oleksii Prohonnyi
 
Angular2 with TypeScript
Angular2 with TypeScript Angular2 with TypeScript
Angular2 with TypeScript
Rohit Bishnoi
 
Intro To React Native
Intro To React NativeIntro To React Native
Intro To React Native
FITC
 
Beyond AngularJS: Best practices and more
Beyond AngularJS: Best practices and moreBeyond AngularJS: Best practices and more
Beyond AngularJS: Best practices and more
Ari Lerner
 
React Native - Getting Started
React Native - Getting StartedReact Native - Getting Started
React Native - Getting Started
Tracy Lee
 
Quick introduction to Angular 4 for AngularJS 1.5 developers
Quick introduction to Angular 4 for AngularJS 1.5 developersQuick introduction to Angular 4 for AngularJS 1.5 developers
Quick introduction to Angular 4 for AngularJS 1.5 developers
Paweł Żurowski
 
Dive into Angular, part 1: Introduction
Dive into Angular, part 1: IntroductionDive into Angular, part 1: Introduction
Dive into Angular, part 1: Introduction
Oleksii Prohonnyi
 
An Overview of Angular 4
An Overview of Angular 4 An Overview of Angular 4

What's hot (20)

Angular 2 - An Introduction
Angular 2 - An IntroductionAngular 2 - An Introduction
Angular 2 - An Introduction
 
Get rid of controllers in angular 1.5.x start using component directives
Get rid of controllers in angular 1.5.x start using component directivesGet rid of controllers in angular 1.5.x start using component directives
Get rid of controllers in angular 1.5.x start using component directives
 
Web components are the future of the web - Take advantage of new web technolo...
Web components are the future of the web - Take advantage of new web technolo...Web components are the future of the web - Take advantage of new web technolo...
Web components are the future of the web - Take advantage of new web technolo...
 
What is Angular version 4?
What is Angular version 4?What is Angular version 4?
What is Angular version 4?
 
What are the components in React?
What are the components in React?What are the components in React?
What are the components in React?
 
Angular 4 - quick view
Angular 4 - quick viewAngular 4 - quick view
Angular 4 - quick view
 
Angular 5
Angular 5Angular 5
Angular 5
 
Angular 4 and TypeScript
Angular 4 and TypeScriptAngular 4 and TypeScript
Angular 4 and TypeScript
 
Routing & Navigating Pages in Angular 2
Routing & Navigating Pages in Angular 2Routing & Navigating Pages in Angular 2
Routing & Navigating Pages in Angular 2
 
Developing a Demo Application with Angular 4 - J2I
Developing a Demo Application with Angular 4 - J2IDeveloping a Demo Application with Angular 4 - J2I
Developing a Demo Application with Angular 4 - J2I
 
Powershell of component object model COM Hijacking
Powershell of component object model COM Hijacking Powershell of component object model COM Hijacking
Powershell of component object model COM Hijacking
 
Understand components in Angular 2
Understand components in Angular 2Understand components in Angular 2
Understand components in Angular 2
 
Dive into Angular, part 3: Performance
Dive into Angular, part 3: PerformanceDive into Angular, part 3: Performance
Dive into Angular, part 3: Performance
 
Angular2 with TypeScript
Angular2 with TypeScript Angular2 with TypeScript
Angular2 with TypeScript
 
Intro To React Native
Intro To React NativeIntro To React Native
Intro To React Native
 
Beyond AngularJS: Best practices and more
Beyond AngularJS: Best practices and moreBeyond AngularJS: Best practices and more
Beyond AngularJS: Best practices and more
 
React Native - Getting Started
React Native - Getting StartedReact Native - Getting Started
React Native - Getting Started
 
Quick introduction to Angular 4 for AngularJS 1.5 developers
Quick introduction to Angular 4 for AngularJS 1.5 developersQuick introduction to Angular 4 for AngularJS 1.5 developers
Quick introduction to Angular 4 for AngularJS 1.5 developers
 
Dive into Angular, part 1: Introduction
Dive into Angular, part 1: IntroductionDive into Angular, part 1: Introduction
Dive into Angular, part 1: Introduction
 
An Overview of Angular 4
An Overview of Angular 4 An Overview of Angular 4
An Overview of Angular 4
 

Similar to React with Ref

Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
StephieJohn
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
Tariqul islam
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
Remo Jansen
 
React
ReactReact
Corso su ReactJS
Corso su ReactJSCorso su ReactJS
Corso su ReactJS
LinkMe Srl
 
React Best Practices All Developers Should Follow in 2024.pdf
React Best Practices All Developers Should Follow in 2024.pdfReact Best Practices All Developers Should Follow in 2024.pdf
React Best Practices All Developers Should Follow in 2024.pdf
BOSC Tech Labs
 
A React Journey
A React JourneyA React Journey
A React Journey
LinkMe Srl
 
20 React Interview Questions. Pdf Download
20 React Interview Questions. Pdf Download20 React Interview Questions. Pdf Download
20 React Interview Questions. Pdf Download
Mohd Quasim
 
ReactJS.pptx
ReactJS.pptxReactJS.pptx
ReactJS.pptx
SamyakShetty2
 
Techpaathshala ReactJS .pdf
Techpaathshala ReactJS .pdfTechpaathshala ReactJS .pdf
Techpaathshala ReactJS .pdf
Techpaathshala
 
React patterns
React patternsReact patterns
React patterns
Naimish Verma
 
Copy of React_JS_Notes.pdf
Copy of React_JS_Notes.pdfCopy of React_JS_Notes.pdf
Copy of React_JS_Notes.pdf
suryanarayana272799
 
React js - The Core Concepts
React js - The Core ConceptsReact js - The Core Concepts
React js - The Core Concepts
Divyang Bhambhani
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
Chiew Carol
 
How to create components in react js
How to create components in react jsHow to create components in react js
How to create components in react js
BOSC Tech Labs
 
Welcome to React & Flux !
Welcome to React & Flux !Welcome to React & Flux !
Welcome to React & Flux !Ritesh Kumar
 
React, Flux and more (p1)
React, Flux and more (p1)React, Flux and more (p1)
React, Flux and more (p1)
tuanpa206
 
React a11y-csun
React a11y-csunReact a11y-csun
React a11y-csun
Poonam Tathavadkar
 
React js
React jsReact js
React js
Alireza Akbari
 

Similar to React with Ref (20)

Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
 
React
ReactReact
React
 
Corso su ReactJS
Corso su ReactJSCorso su ReactJS
Corso su ReactJS
 
React Best Practices All Developers Should Follow in 2024.pdf
React Best Practices All Developers Should Follow in 2024.pdfReact Best Practices All Developers Should Follow in 2024.pdf
React Best Practices All Developers Should Follow in 2024.pdf
 
A React Journey
A React JourneyA React Journey
A React Journey
 
20 React Interview Questions. Pdf Download
20 React Interview Questions. Pdf Download20 React Interview Questions. Pdf Download
20 React Interview Questions. Pdf Download
 
ReactJS.pptx
ReactJS.pptxReactJS.pptx
ReactJS.pptx
 
Techpaathshala ReactJS .pdf
Techpaathshala ReactJS .pdfTechpaathshala ReactJS .pdf
Techpaathshala ReactJS .pdf
 
React patterns
React patternsReact patterns
React patterns
 
Copy of React_JS_Notes.pdf
Copy of React_JS_Notes.pdfCopy of React_JS_Notes.pdf
Copy of React_JS_Notes.pdf
 
React js - The Core Concepts
React js - The Core ConceptsReact js - The Core Concepts
React js - The Core Concepts
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
 
reatppt.pptx
reatppt.pptxreatppt.pptx
reatppt.pptx
 
How to create components in react js
How to create components in react jsHow to create components in react js
How to create components in react js
 
Welcome to React & Flux !
Welcome to React & Flux !Welcome to React & Flux !
Welcome to React & Flux !
 
React, Flux and more (p1)
React, Flux and more (p1)React, Flux and more (p1)
React, Flux and more (p1)
 
React a11y-csun
React a11y-csunReact a11y-csun
React a11y-csun
 
React js
React jsReact js
React js
 

More from Jack Forbes

Authentication and Authorization Defined_ What's the Difference_.pdf
Authentication and Authorization Defined_ What's the Difference_.pdfAuthentication and Authorization Defined_ What's the Difference_.pdf
Authentication and Authorization Defined_ What's the Difference_.pdf
Jack Forbes
 
Digital identity management for the cmo, ciso, and cio
Digital identity management for the cmo, ciso, and cioDigital identity management for the cmo, ciso, and cio
Digital identity management for the cmo, ciso, and cio
Jack Forbes
 
Step up authentication
Step up authenticationStep up authentication
Step up authentication
Jack Forbes
 
Why do consumers prefer social login
Why do consumers prefer social loginWhy do consumers prefer social login
Why do consumers prefer social login
Jack Forbes
 
How secure is two factor authentication (2 fa)
How secure is two factor authentication (2 fa)How secure is two factor authentication (2 fa)
How secure is two factor authentication (2 fa)
Jack Forbes
 
Future of bring your own identity
Future of bring your own identityFuture of bring your own identity
Future of bring your own identity
Jack Forbes
 
Cms api integrations list-LoginRadius
Cms api integrations list-LoginRadiusCms api integrations list-LoginRadius
Cms api integrations list-LoginRadius
Jack Forbes
 
Buyer’s guide to multi factor authentication
Buyer’s guide to multi factor authenticationBuyer’s guide to multi factor authentication
Buyer’s guide to multi factor authentication
Jack Forbes
 
8 effective communication strategies for internal alignment and growth in 2021
8 effective communication strategies for internal alignment and growth in 20218 effective communication strategies for internal alignment and growth in 2021
8 effective communication strategies for internal alignment and growth in 2021
Jack Forbes
 
Adaptive risk based authentication
Adaptive risk based authenticationAdaptive risk based authentication
Adaptive risk based authentication
Jack Forbes
 
Social login-infographic
Social login-infographicSocial login-infographic
Social login-infographic
Jack Forbes
 
Login radius identity brokering infographic
Login radius identity brokering infographicLogin radius identity brokering infographic
Login radius identity brokering infographic
Jack Forbes
 
Web and Mobile SSO
Web and Mobile SSOWeb and Mobile SSO
Web and Mobile SSO
Jack Forbes
 
Loginradius phone authentication
Loginradius phone authenticationLoginradius phone authentication
Loginradius phone authentication
Jack Forbes
 
Identity and Access Management (IAM)
Identity and Access Management (IAM)Identity and Access Management (IAM)
Identity and Access Management (IAM)
Jack Forbes
 
Social Login Explained
Social Login ExplainedSocial Login Explained
Social Login Explained
Jack Forbes
 
A Guide to Multi Factor Authentication
A Guide to Multi Factor AuthenticationA Guide to Multi Factor Authentication
A Guide to Multi Factor Authentication
Jack Forbes
 
Guide to MFA
Guide to MFAGuide to MFA
Guide to MFA
Jack Forbes
 
Ciam for devops
Ciam for devops Ciam for devops
Ciam for devops
Jack Forbes
 
Defining the omnichannel customer experience
Defining the omnichannel customer experienceDefining the omnichannel customer experience
Defining the omnichannel customer experience
Jack Forbes
 

More from Jack Forbes (20)

Authentication and Authorization Defined_ What's the Difference_.pdf
Authentication and Authorization Defined_ What's the Difference_.pdfAuthentication and Authorization Defined_ What's the Difference_.pdf
Authentication and Authorization Defined_ What's the Difference_.pdf
 
Digital identity management for the cmo, ciso, and cio
Digital identity management for the cmo, ciso, and cioDigital identity management for the cmo, ciso, and cio
Digital identity management for the cmo, ciso, and cio
 
Step up authentication
Step up authenticationStep up authentication
Step up authentication
 
Why do consumers prefer social login
Why do consumers prefer social loginWhy do consumers prefer social login
Why do consumers prefer social login
 
How secure is two factor authentication (2 fa)
How secure is two factor authentication (2 fa)How secure is two factor authentication (2 fa)
How secure is two factor authentication (2 fa)
 
Future of bring your own identity
Future of bring your own identityFuture of bring your own identity
Future of bring your own identity
 
Cms api integrations list-LoginRadius
Cms api integrations list-LoginRadiusCms api integrations list-LoginRadius
Cms api integrations list-LoginRadius
 
Buyer’s guide to multi factor authentication
Buyer’s guide to multi factor authenticationBuyer’s guide to multi factor authentication
Buyer’s guide to multi factor authentication
 
8 effective communication strategies for internal alignment and growth in 2021
8 effective communication strategies for internal alignment and growth in 20218 effective communication strategies for internal alignment and growth in 2021
8 effective communication strategies for internal alignment and growth in 2021
 
Adaptive risk based authentication
Adaptive risk based authenticationAdaptive risk based authentication
Adaptive risk based authentication
 
Social login-infographic
Social login-infographicSocial login-infographic
Social login-infographic
 
Login radius identity brokering infographic
Login radius identity brokering infographicLogin radius identity brokering infographic
Login radius identity brokering infographic
 
Web and Mobile SSO
Web and Mobile SSOWeb and Mobile SSO
Web and Mobile SSO
 
Loginradius phone authentication
Loginradius phone authenticationLoginradius phone authentication
Loginradius phone authentication
 
Identity and Access Management (IAM)
Identity and Access Management (IAM)Identity and Access Management (IAM)
Identity and Access Management (IAM)
 
Social Login Explained
Social Login ExplainedSocial Login Explained
Social Login Explained
 
A Guide to Multi Factor Authentication
A Guide to Multi Factor AuthenticationA Guide to Multi Factor Authentication
A Guide to Multi Factor Authentication
 
Guide to MFA
Guide to MFAGuide to MFA
Guide to MFA
 
Ciam for devops
Ciam for devops Ciam for devops
Ciam for devops
 
Defining the omnichannel customer experience
Defining the omnichannel customer experienceDefining the omnichannel customer experience
Defining the omnichannel customer experience
 

Recently uploaded

Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
ViralQR
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 

Recently uploaded (20)

Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 

React with Ref

  • 1. React with Ref In this article, We will discuss the manipulation of DOM elements with Ref directly with React. React Framework builds your components and abstracts your code away from manipulation within the DOM but still leaves the door open for developers to access it. Reason are few cases where it might be necessary. That's why React provides an escape hatch know as refs.
  • 2. Refs are a function that use to access the DOM from components. You only need to attach a ref to the element in your application to provide access to it from anywhere within your component without making use of props and all. We can also use Refs to direct access to React elements and use callbacks with them. We should only use refs when the required interaction cannot be achieved using state and props. Use Refs We can use refs to do anything that needs to be manipulated in the DOM. Some good cases like focus, test selection, media playback, triggering mandatory animations, or integration with the third-party DOM library. Note: We should avoid using refs because it removes the purpose of using React. For example, If you want to show and hide a popup component. We should use a boolean prop for it instead of manipulating dom. Creating Refs
  • 3. We can use React.createRef()method to create Refs, and then we can attach to a Dom element via the ref attribute after that, we can access and modify that element through the ref. class App extends React.Component { constructor(props) { super(props); this.myRef = React.createRef(); } render() { return <div ref={this.myRef} />; } } In above code, We created this.myRef in the constructor by calling React.createRef() method. Then in the render method , we attached the returned value to ref of the div element, a reference to the node becomes accessible at the current attribute of the ref. We should not use ref attribute on function components because they do not have instances. React will assign the current property with Dom element when component mount and assign null to it when component unmount. ref updates happen before componentDidMount or componentDidUpdate methods.
  • 4. We can pass refs as props to the component. Example: function MyCustomTextInput ({ myInputRef }) { return <input ref={myInputRef} />; } class App extends React.Component { constructor(props) { super(props); this.myInputRef = React.createRef(); } componentDidMount() { this.myInputRef.current.focus(); } render() { return <MyCustomTextInput inputRef={this.myInputRef} />; } } In above code, App passed its ref as props to MyCustomTextInput component. You can read more in detail about call back ref in this tutorial about React with Ref.