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.

Refs do not work properly in docs mode (mdx)

See original GitHub issue

Describe the bug Only in docs mode (not canvas mode), a ref does not contain a reference to the element it’s attached to.

Notes from @shilman :

it’s probably related to react context. in storybook each story is a function, and react hooks down’t work in normal functions alone. however, when those functions are wrapped in React.createElement, hooks are supported. In JSX it’s the difference between {storyFn()} and <StoryFn />. It’s possible that MDX needs to do the latter when the story is inline. Mind filing an issue on this with a simple repro?

To Reproduce

  1. Create the following story (in an .mdx file):
  <Story name="Bug">
    {() => { 
      const ref = useRef(null);
      return (
        <>
          <div ref={ref} style={{ overflow: "hidden" }} />
          <button onClick={() => console.log(ref.current)}>Show ref value</button>
        </>
      );
    }}
  </Story>
  1. Open your console
  2. Go to the canvas tab and click on the “Show ref value” button… In Chrome, it should log out <div></div>.
  3. Check docs tab and click on the “Show ref value” button. In Chrome, it should log null.

Expected behavior I expect to log an instance of the element the ref is on in both canvas and docs mode.

Screenshots N/A

System:

Environment Info:

  System:
    OS: macOS Mojave 10.14.6
    CPU: (8) x64 Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
  Binaries:
    Node: 12.16.0 - /usr/local/bin/node
    Yarn: 1.19.1 - /usr/local/bin/yarn
    npm: 6.13.4 - /usr/local/bin/npm
  Browsers:
    Chrome: 80.0.3987.163
    Firefox: 67.0.1
    Safari: 13.1
  npmPackages:
    @storybook/addon-a11y: ^6.0.0-alpha.30 => 6.0.0-alpha.30 
    @storybook/addon-actions: ^6.0.0-alpha.30 => 6.0.0-alpha.30 
    @storybook/addon-backgrounds: ^6.0.0-alpha.30 => 6.0.0-alpha.30 
    @storybook/addon-centered: ^6.0.0-alpha.30 => 6.0.0-alpha.30 
    @storybook/addon-docs: ^6.0.0-alpha.30 => 6.0.0-alpha.30 
    @storybook/addon-knobs: ^6.0.0-alpha.30 => 6.0.0-alpha.30 
    @storybook/addon-links: ^6.0.0-alpha.30 => 6.0.0-alpha.30 
    @storybook/addons: ^6.0.0-alpha.30 => 6.0.0-alpha.30 
    @storybook/react: ^6.0.0-alpha.30 => 6.0.0-alpha.30 
    @storybook/theming: ^6.0.0-alpha.30 => 6.0.0-alpha.30 

Additional context N/A

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
nijkcommented, Oct 22, 2020

FWIW I’ve found a potential workaround the the originally reported issue by using a wrapper component with render props, although this doesn’t fix the JSS dynamic values issue.

<Story name="Workaround">
  {() => {
    const Wrapper = ({ children }) => {
      const ref = useRef(null);
      return (
        <>
          <div ref={ref} style={{ overflow: "hidden" }} />
          {children({ ref })}
        </>
      )
    };
    return (
      <Wrapper>
        {({ ref }) => (
          <button onClick={() => console.log(ref.current)}>Show ref value</button>
        )}
      </Wrapper>
    );
  }}
</Story>
1reaction
shilmancommented, Oct 16, 2020

@nijk bad markdown formatting. updated

Read more comments on GitHub >

github_iconTop Results From Across the Web

Refs do not work properly in docs mode (mdx) #10446
Describe the bug Only in docs mode (not canvas mode), a ref does not contain a reference to the element it's attached to....
Read more >
storybook/addon-docs - npm
DocsPage is a zero-config aggregation of your component stories, text descriptions, docgen comments, props tables, and code examples into clean, ...
Read more >
Rich docs with Storybook MDX
Which is why at Storybook, we're hell-bent on making UI component docs fast and easy. Our first step was DocsPage, a tool to...
Read more >
Basic Features: Fast Refresh - Next.js
Next.js' Fast Refresh is a new hot reloading experience that gives you instantaneous feedback on edits made to your React components.
Read more >
How to use links in documentation - Contributor guide
Do not use "click here" as link text. It's bad for search engine optimization and doesn't adequately describe the target. Correct:.
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