Questions tagged [use-ref]

"useRef" is a React hook which returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). The returned object will persist for the full lifetime of the component.

Docs

https://reactjs.org/docs/hooks-reference.html#useref

Code example

function TextInputWithFocusButton() {
  const inputEl = useRef(null);
  const onButtonClick = () => {
    // `current` points to the mounted text input element
    inputEl.current.focus();
  };
  return (
    <>
      <input ref={inputEl} type="text" />
      <button onClick={onButtonClick}>Focus the input</button>
    </>
  );
}
573 questions
25
votes
3 answers

What are production use cases for the useRef, useMemo, useCallback hooks?

Outside of the counter example seen in many YouTube tutorial videos, what are practical/real-world use cases for useMemo and useCallback? Also, I've only seen an input focus example for the useRef hook. Please share other use cases you've found for…
ln09nv2
  • 965
  • 4
  • 19
  • 35
16
votes
3 answers

Cannot assign to read only property 'current' in React useRef

i used react useRef in functional components to get link on html object and store it in Recoil atom. For example: const Children = () => { const [refLink, setSrefLink] = useRecoilState(refLink) return } const Parent = ()…
dendroid
  • 181
  • 1
  • 1
  • 7
16
votes
3 answers

useRef to store previous state value

I am confused about the below usage of useRef to store the previous state value. Essentially, how is it able to display the previous value correctly. Since the useEffect has a dependency on "value", my understanding was that each time "value"…
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
15
votes
5 answers

Which is the right way to detect first render in a react component

I have a scenario where I need to detect the first render of a component. Here I have build a small example. Could someone explain to me what is the correct approach? Why do most of the people suggest to use a ref instead of a plain…
Deepak Kumar Padhy
  • 4,128
  • 6
  • 43
  • 79
12
votes
4 answers

useRef for element in loop in react

Using React, i have a list of ref statically declared this way: let line1 = useRef(null); let line2 = useRef(null); let line3 = useRef(null); ... //IN MY RENDER PART
  • (line1 = el)}>line1
  • (line2 =…
  • popeating
    • 386
    • 1
    • 2
    • 16
    10
    votes
    1 answer

    How can I define the type for a

    I'm trying to control the play/pause state of a video using ref's in React.js, my code works but there are tslint errors I am trying to work through: function App() { const playVideo = (event:any) => { video.current.play() } …
    Jake Chambers
    • 513
    • 2
    • 6
    • 22
    10
    votes
    2 answers

    React: How to wait until ref is available when inserted (rendered) within a second container

    EDIT: better explanation The context: I receive some plain HTML code from a 3rd server, which I want to insert in my React app modify it The vanilla JS approach I can modify the string with regex and add any HTML tag with an id Then I can modify…
    GWorking
    • 4,011
    • 10
    • 49
    • 90
    7
    votes
    1 answer

    How do you use refs to access the values of mapped children in React?

    I have an app that pulls data from a GraphQL database and then .maps it into custom form components (quantity number textboxes). Right now, the components themselves are holding state of their individual quantities, but I need to be able to access…
    ep84
    • 315
    • 2
    • 17
    7
    votes
    1 answer

    Avoid parent component re-rendering when Child component updates parent state,

    In our react application, We have parent-child component. Child component calls parent method to update parent state values. Here is sample code //Parent component const parent = ({ items }) => { const [information, setInformation] =…
    Alex
    • 1,406
    • 2
    • 18
    • 33
    7
    votes
    5 answers

    React - Trigger form submit using useRef

    Good day, so I am trying to perform an intermediate function before a form action="POST" takes place. Ive tried two things firstly onSubmit={functionName} but the form always performs the action even if in the onSubmit function I return false.…
    J.Naude
    • 125
    • 1
    • 4
    • 11
    6
    votes
    1 answer

    React native 'BottomSheetModalInternalContext' cannot be null

    I'm using the BottomSheetModal to display a list of all countries in a bottom sheet. But I get the error message 'BottomSheetModalInternalContext' cannot be null! when navigating to the screen that includes the bottom modal. I assume that the error…
    JonasLevin
    • 1,592
    • 1
    • 20
    • 50
    6
    votes
    1 answer

    useRef throws not assignable to type LegacyRef | undefined

    I'm using react-slick-slider and what I want to achieve is to make custom arrows. So code looks like this: const FeedbackSlider = () => { const [isLargeScreen] = useMediaQuery("(min-width: 1050px)") const sliderRef = useRef() const prev =…
    Marko B.
    • 535
    • 3
    • 8
    • 18
    6
    votes
    3 answers

    Why is typescript complaining when trying to use ref in react?

    I am using ref to animate elements on scroll. const foo = () => { if (!ref.current) return; const rect = ref.current.getBoundingClientRect(); setAnimClass( rect.top >= 0 && rect.bottom <= window.innerHeight ? styles.animClass :…
    Mayank Kumar Chaudhari
    • 16,027
    • 10
    • 55
    • 122
    6
    votes
    1 answer

    React: Remove focus from input element on escape key

    I have an input tag, when I press the escape key, I want the focus on the input element to go away, meaning there is no longer a text cursor or any focus styling(when the user types, their input will not go into the input tag). The code below is my…
    Sam
    • 1,765
    • 11
    • 82
    • 176
    6
    votes
    1 answer

    How do I pass ref to a neighbour component

    I am trying to use ref on a search input in my Header component which ISN'T a higher order component to my ResultsList component. I want to set focus on the Header's search input from the ResultsList component. It is intuitive from the Header…
    LazioTibijczyk
    • 1,701
    • 21
    • 48
    1
    2 3
    38 39