question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

picking `ref` from component's props is not allowed

See original GitHub issue

Description

Picking ref from component’s props produces an error. Picking it is necessary to assign proper type to innerRef.

Error:

Warning: [object Object]: `ref` is not a prop. Trying to access it will result in `undefined` being returned. 
If you need to access the same value within the child component, you should pass it as a different prop.
(https://fb.me/react-special-props)

Reproduction

My code is similar to example code from Readme.md section on how to pass ref:

export const Box = React.forwardRef(
  <E extends React.ElementType = typeof defaultElement>(
    { ref, ...restProps }: BoxProps<E>,
    innerRef: typeof ref
  ) => {
    return <StyledBox ref={innerRef} as={defaultElement} {...restProps} />;
  }
) as <E extends React.ElementType = typeof defaultElement>(
  props: BoxProps<E>
) => JSX.Element;

Expected behavior

innerRef should be somehow typed without picking ref from component’s props

Environment

I am using Next.js 9.4.4 and React 16.13.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
kripodcommented, Jul 16, 2020

I’m sorry, CodeSandbox didn’t show the error for me. It turns out that the ref prop shall not be destructured, but kept inside ...restProps, as follows:

import { Box } from "react-polymorphic-box";

export const Heading = React.forwardRef(
  <E extends React.ElementType = typeof defaultElement>(
+    { color, style, ...restProps }: HeadingProps<E>,
+    innerRef: typeof restProps.ref
-    { ref, color, style, ...restProps }: HeadingProps<E>,
-    innerRef: typeof ref
  ) => {
    return (
      <Box
-       ref={innerRef}
        as={defaultElement}
        style={{ color, ...style }}
        {...restProps}
+       ref={innerRef}
      />
    );
  }
) as <E extends React.ElementType = typeof defaultElement>(
  props: HeadingProps<E>
) => JSX.Element;

I’ve updated the readme and my playground to reflect these changes. Also, I’ve fixed the example of @rajatsharma again.

2reactions
rajatsharmacommented, Jul 16, 2020

Here’s the CodeSandbox for my example: https://codesandbox.io/s/cocky-lehmann-2iy2t?file=/src/Box.tsx

Read more comments on GitHub >

github_iconTop Results From Across the Web

refs field on custom react component not allowed · Issue #5347
The issue is that the props for ComponentA are determined by the props that you specified when creating ComponentA. When you say class ......
Read more >
ref does not exist on container component - Stack Overflow
This error tells you that you cannot pass the prop ref to the component SearchBox because ref is not defined as a prop....
Read more >
Forwarding Refs - React
Regular function or class components don't receive the ref argument, and ref is not available in props either. Ref forwarding is not limited...
Read more >
What is Ref Forwarding? - In Plain English
The Preferred Solution: React.​​ forwardRef() we can pass a second argument, ref, in addition to props that can then be utilized within the...
Read more >
Useful Patterns by Use Case - React TypeScript Cheatsheets
Forwarding Refs: As the React docs themselves note, most usecases will not need to obtain a ref ... Strategy: extract a component's props...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found