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.

MDXCreateElement breaks forwardRef()

See original GitHub issue

Subject 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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
atomikscommented, Aug 16, 2019

I think I found a fix with node_modules debugging.

Wrap MDXCreateElement itself in forwardRef like this:

const MDXCreateElement = forwardRef((props, ref) => {
  const {
    components: propComponents,
    mdxType,
    originalType,
    parentName,
    ...etc
  } = props;

  const components = useMDXComponents(propComponents);
  const type = mdxType;
  const Component =
    components[`${parentName}.${type}`] ||
    components[type] ||
    DEFAULTS[type] ||
    originalType;

  if (propComponents) {
    return React.createElement(Component, {
      ref,
      ...etc,
      components: propComponents
    });
  }

  return React.createElement(Component, {ref, ...etc});
});

Seems to fix the issue for me.

0reactions
johnocommented, Sep 3, 2019

Fixed by #751 and released as @mdx-js/react@1.4.2.

Read more comments on GitHub >

github_iconTop 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 >

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