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.

Storybook Preact story rendering breaks when using hooks to declare state in stories

See original GitHub issue

Describe the bug Using preact/hooks inside of story functions breaks Storybook for Preact story rendering.

To Reproduce

The following story errors out:

import { h } from 'preact';
import { useState } from 'preact/hooks';

export default {
  title: 'Sample story'
};

export const Bug = () => {
  const [state, setState] = useState(false);
  return (
    <button onClick={() => setState(!state)}>{state ? 'true' : 'false'}</button>
  );
}

If you declare state in another function as a functional component, the story works:

import { h } from 'preact';
import { useState } from 'preact/hooks';

export default {
  title: 'Sample story'
};

const Template = () => {
  const [state, setState] = useState(false);
  return (
    <button onClick={() => setState(!state)}>{state ? 'true' : 'false'}</button>
  );
}

export const NoBug = () => <Template/>

Expected behavior There’s no clear indication in the documentation that that might not be allowed. It might be worth mentioning that somewhere in Writing Stories sections.

Screenshots N/A

Code snippets See above.

System The npx sb@next info command fails because of integrity checks, but I am using Storybook 6.1.17.

"@storybook/addon-actions": "6.1.17",
"@storybook/addon-essentials": "6.1.17",
"@storybook/addon-links": "6.1.17",
"@storybook/preact": "6.1.17",
"storybook-addon-designs": "5.4.3",
"storybook-addon-themes": "6.0.1",

Additional context None.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
shilmancommented, Apr 8, 2021

Ermahgerd!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.3.0-alpha.1 containing PR #14473 that references this issue. Upgrade today to the @next NPM tag to try it out!

npx sb upgrade --prerelease
1reaction
KrofDrakulacommented, Apr 4, 2021

It looks like there’s a problem in the preact kitchen sink app. In package.json, we have:

  "devDependencies": {
    "preact": "^10.5.9"
  },
  "peerDependencies": {
    "@babel/core": "*",
    "preact": "^8.0.0||^10.0.0"
  },

This installs preact as a dev dependency when bootstrapping, so when client/preview/render.tsx imports the preact package, it would always import preact^10.5.9. If the app uses 8.x, the mismatch between VNode types in 8.x and 10.x would explain why the kitchen sink app doesn’t work in development, since it uses 8.x. Upgrading preact to 10.x on the kitchen sink app would make it work.

After fixing this problem, the next step will be solving the original hooks problem mentioned in this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Test runner Addon | Storybook: Frontend workshop for UI ...
The test runner is simple in design – it just visits each story from a running Storybook instance and makes sure the component...
Read more >
Newest 'storybook-addon-specifications' Questions
Hi i am using storybook in react, For me i am facing issue in configuring firebase and state in storybook, Firebase is getting...
Read more >
@storybook/core-events | Yarn - Package Manager
NOTE: For Angular users using inline story rendering in addon-docs, this is a breaking prerelease change. See below and apologies for the back...
Read more >
storybookjs/storybook (Raised $170.00) - Issuehunt
[Bug]: Next.js 13 hook useSelectedLayoutSegment breaks storybook. Unfunded#20320created by ... MDX does not display the source snippet when using render.
Read more >
Preact Signals and React's maintainers' view : r/reactjs - Reddit
Local state change to a component does not cause complete re-rendering of the hierarchy btw. So no memo needed. It only re-renders that ......
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