MDXCreateElement breaks forwardRef()
See original GitHub issueSubject of the issue
I’m not sure if this is gatsby-mdx
or this repo, so forgive me here. I upgraded from v0 to v1 and am experiencing this problem. v0 seemed to work okay.
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
Your environment
- macOS 10.14.4
"@mdx-js/mdx": "^1.0.19",
"@mdx-js/react": "^1.0.16",
"gatsby-mdx": "^0.6.3",
- Node 10, npm 6.8
Steps to reproduce
I don’t actually know if this reproduces (haven’t tested sorry 😥 ), but this is the general idea so hopefully it gives pointers:
Framework.js:
import {useRef, cloneElement} from 'react';
import styled from 'styled-components';
export function Parent({ children }) {
const ref = useRef();
return cloneElement(children, {
ref(node) {
ref.current = node;
}
});
}
export const Child = styled.button``;
index.mdx:
import {Parent, Child} from '../components/Framework';
<Parent>
<Child />
</Parent>
I think the <Child />
is no longer wrapped in forwardRef
, causing the error message
Expected behaviour
MDXCreateElement should keep forwardRef
return value so the element can be given a ref
Actual behaviour
It doesn’t?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Using useRef() in a Storybook preview - Stack Overflow
Attempts to access this ref will fail. Did you mean to use React.forwardRef()? Check the render method of `MDXCreateElement`.
Read more >react-dom.development.js:18687 the above error occurred in ...
I'm trying to use a react-router, after using the library some problems begin, I've already tried to write different code, I found it...
Read more >gatsby - UNPKG
__esModule = true;","function _nonIterableRest() {\n throw new ... the innerFn has the same effect as\n // breaking out of the dispatch switch statement....
Read more >storybookjs/storybook (Raised $170.00) - Issuehunt
[Bug]: Next.js 13 hook useSelectedLayoutSegment breaks storybook ... getStorybook() errors with "Cannot read properties of undefined (reading 'stories')".
Read more >ForwardRef component • storybookjs/storybook - Chromatic
ForwardRef. fix-colorsjs-53 – Build 24450. Displays forwarded ref components correctly. Uses forwardRef displayName if available.
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 think I found a fix with node_modules debugging.
Wrap
MDXCreateElement
itself inforwardRef
like this:Seems to fix the issue for me.
Fixed by #751 and released as
@mdx-js/react@1.4.2
.