Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4711

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T02:31:09+00:00 2024-11-27T02:31:09+00:00

useRef helps you to avoid re-rendering of the component

  • 61k

useRef can store any mutable data that you want to persist between renders. It helps you to avoid re-rendering of the component when you don't want to.

useRef vs useState

useRef is similar to useState but there are some differences,

  • both useState and useRef can maintain the value between renders.
  • useState is used to store the state of a component whereasuseRef is used to store any mutable value.
  • useState will cause the component to re-render when the state is updated whereas useRef will not cause the component to re-render when the value is updated.
const [text, setText] = useState('') const handleTextChange = (e) => {   setText(e.target.value) }  // re-renders the component on every text change return (   <div>     <input type="text" value={text} onChange={handleTextChange} />   </div> ) 
Enter fullscreen mode Exit fullscreen mode

Using useRef to store the value of the input element will not cause the component to re-render when the value is updated.

const textRef = useRef('') const handleTextChange = (e) => {   textRef.current = e.target.value }  // no re-renders when the text changes return (   <div>     <input type="text" value={textRef.current} onChange={handleTextChange} />   </div> ) 
Enter fullscreen mode Exit fullscreen mode

useRef is mostly used for,

  • storing the previous value of a state or props of a component across rendering of a component
  • accessing DOM nodes in React

useRef to store the previous value of a state or props of a component

import React, { useState, useRef } from 'react'  const Slide = ({ selectedSlideIndex }) => {   const [slide, setSlide] = useState(selectedSlideIndex)   const prevSlideRef = useRef()    // update the previous slide value when the slide value changes   useEffect(() => {     prevSlideRef.current = slide   }, [slide])    const prevSlide = prevSlideRef.current    return (     <div>       <p>Current slide: {slide}</p>       <p>Previous slide: {prevSlide}</p>       <button onClick={() => setSlide(slide + 1)}>Next</button>     </div>   ) } 
Enter fullscreen mode Exit fullscreen mode

In the above example, useRef helps to maintain the previous value of the slide state variable.

Some of the use cases are,

  • undo/redo functionality
  • maintaining the previous value of a state variable to compare with the current value and perform some action
    • saving a draft if value of a textarea is changed
    • showing a confirmation dialog if the value of a textarea is changed and the user tries to navigate away from the page

useRef to access DOM nodes

import React, { useRef, useEffect } from 'react'  const Input = () => {   const inputRef = useRef(null)    // focus the input element when the component is mounted   useEffect(() => {     inputRef.current.focus()   }, [])    return (     <div>       <input ref={inputRef} type="text" />     </div>   ) } 
Enter fullscreen mode Exit fullscreen mode

In the above example, useRef helps to access the input element and focus it when the component is mounted.

Some of the use cases are,

  • focusing or blurring an element
  • accessing the DOM node to get the size of an element
  • accessing the DOM node to get the scroll position of an element

Hope this helps to learn the magic of useRef hook. Happy referencing 😃

beginnersreacttutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.