Questions tagged [react-forwardref]

162 questions
36
votes
4 answers

Using forwardRef with proptypes and eslint

I am trying to use forwardRef for a Button in a project using eslint and prop-types. This is what I tried so far, and the errors I get each time: First attempt function Button ({ action = { callback: () => {}, title: 'unknown' } }, ref) { return…
Sharcoux
  • 5,546
  • 7
  • 45
  • 78
23
votes
1 answer

TypeError: Component is not a function on adding forwardRef

Hi I was trying to add forwardRef to a child component but getting the following on adding the forwardRef: TypeError: Component is not a function The component is defined as below: import React from 'react'; import { forwardRef } from…
21
votes
2 answers

Understanding: Warning: Function components cannot be given refs

I know that refs are used to access DOM elements directly without altering the state. I read that it's not possible to give refs to function components because they don't have a state. Refs cannot be attached to function components. Although, we…
devamat
  • 2,293
  • 6
  • 27
  • 50
18
votes
1 answer

Whats the difference between ForwardRefExoticComponent and ForwardRefRenderFunction in react?

I'm writing a React component which can forward ref to its chilren I found out that for the return type of the function components, I can use ForwardRefExoticComponent and ForwardRefRenderFunction. But I'm not sure whats the difference between…
Jake Lam
  • 3,254
  • 3
  • 25
  • 44
17
votes
3 answers

React/Typescript forwardRef types for an element which returns either an input or textArea

I'm trying to create a generic text input component for our application using react and typescript. I want it to be able to either be an input element or a textarea element based on a given prop. So it looks somewhat like this: import {TextArea,…
picklechips
  • 746
  • 2
  • 8
  • 28
11
votes
3 answers

How to check if a div is overflowing in react functional component

I am trying to find out if a div has overflown text and show show more link if it does. I found this stackoverflow answer to check if a div is overflowing. According to this answer, I need to implement a function which can access styles of the…
troglodyte07
  • 3,598
  • 10
  • 42
  • 66
10
votes
2 answers

React.forwardRef is already possible without it, so what's the use of it?

I'm confused on the point of React.forwardRef. As explained in its documentation, I understand that its main use is for a Parent Component to gain access to DOM elements of the Child Component. But I can already do that without even having to use…
Jordy
  • 415
  • 5
  • 13
9
votes
1 answer

why do we need React forwardRef if we can simple pass the created ref in props?

The correct way of passing refs to child components as per react documentation is like this: import React from 'react'; const Input = React.forwardRef((props, ref) => { React.useEffect(() => { ref.current.focus(); }, []); return
Himmature
  • 106
  • 1
  • 2
8
votes
4 answers

React forwardRef - access ref within component, and in parent

I need to access the ref to a textarea inside a component. Within the component, its easy enough: const MyComponent = () => { const inputRef = useRef(); return