picking `ref` from component's props is not allowed
See original GitHub issueDescription
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:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:I’ve updated the readme and my playground to reflect these changes. Also, I’ve fixed the example of @rajatsharma again.
Here’s the CodeSandbox for my example: https://codesandbox.io/s/cocky-lehmann-2iy2t?file=/src/Box.tsx