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.

Button knob causes a re-render on a HTML story

See original GitHub issue

Describe the bug When you add a button(label, action) knob to a HTML story, clicking the button causes the story to rerender after performing the handler action.

To Reproduce This minimal story code should repro:

import { storiesOf } from '@storybook/html';
import { withKnobs, button } from '@storybook/addon-knobs';

storiesOf('Button bugs', module)
  .addDecorator(withKnobs)
  .add('Rerender', () => {
    const input = document.createElement('input');
    button('Focus input', () => {
        input.ownerDocument.defaultView.focus();
        input.focus();
    });
    return input;
  });

When you click the “Focus input” button knob, the input doesn’t actually get focus. On closer inspection, you can see that this is because the story re-renders and the input that gets focus is destroyed.

Expected behavior I would expect a button action not to automatically re-render a story. If re-rendering is desirable in some cases, then a return false; could be provided to prevent re-render.

System:

  • Framework: html
  • Addons: knobs
  • Version: 5.1.0-alpha.36

Additional context The reproduce story above is a very simple replication of our use case. We’re providing a component with a method that programmatically pulls focus, we are using a button knob but the only way to stop it from re-rendering is to throw an error at the end of the handler function, which is far from desirable.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
shilmancommented, Jul 8, 2019

Gadzooks!! I just released https://github.com/storybookjs/storybook/releases/tag/v5.2.0-alpha.37 containing PR #7197 that references this issue. Upgrade today to try it out!

You can find this prerelease on the @next NPM tag.

Closing this issue. Please re-open if you think there’s still more to do.

1reaction
danrhacommented, Jul 5, 2019

I submitted a pull request several days ago which got approved, so the fix should be up on an upcoming version. @andyearnshaw 's suggestion to prevent re-render by returning false was implemented.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to prevent React from re render? - Stack Overflow
1.You are using form element of html. And since you are using button inside it you need to add e. · 2.While passing...
Read more >
Storybook Addon Knobs (deprecated)
Button knobs cause the story to re-render after the handler fires. You can prevent this by having the handler return false . withKnobs...
Read more >
A Story of a React Re-Rendering Bug - Engineering Blog
The “blur” event, which would cause the element containing our button to re-render, gets triggered right after “mousedown” event.
Read more >
How to check if your component rerendered - and why!
Using React DevTools to find out the cause of a rerender. Now that you can see your component rerendered, the next question is...
Read more >
How To Avoid Performance Pitfalls in React with memo ...
Either the component re-renders more than expected or the component has a data-heavy operation that can cause each render to be slow. Because...
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