React StoryBook动态组件失效问题:Next.js项目中FaqPage组件未在StoryBook中显示
Let's walk through the common issues that might be preventing your TestButton component from showing up in Storybook, and fix them one by one:
1. Verify Storybook's Story Detection Configuration
First, check your .storybook/main.js file to ensure it's configured to scan for your stories files. The stories array needs to include the path to your FaqPage.stories.js. For example:
module.exports = { stories: [ "../**/*.stories.js", // Scans all .stories.js files across your project // Or use a more specific path like "../components/**/*.stories.js" if your files live in a components folder ], addons: [ "@storybook/addon-links", "@storybook/addon-essentials", "@storybook/addon-interactions", "@storybook/nextjs", ], framework: { name: "@storybook/nextjs", options: {}, }, };
If your stories file isn't covered by the path pattern here, Storybook won't find it. Adjust the pattern to match your project's folder structure.
2. Fix Import Path or Filename Issues
Double-check the import in FaqPage.stories.js:
import TestButton from './TestButton'
- Confirm
TestButton.jsis in the exact same directory asFaqPage.stories.js(update the path if it's in a subfolder, e.g.,./ui/TestButton). - Ensure filenames match exactly (case-sensitive! On Linux/macOS,
testbutton.jsis not the same asTestButton.js).
3. Validate Story Export Structure
Your story setup is mostly correct, but let's confirm a few details:
- Your default export (story metadata) has the required
titleandcomponentfields, which is good—this will place your story under a "Button" category in the Storybook sidebar:const FaqPage = { title: 'Button', component: TestButton, } export default FaqPage - A minor note that won't break visibility but will fix styling: your
primaryarg is a string'true'instead of a booleantrue. Update it to:Index.args = { primary: true, // Use boolean instead of string label: 'Hello', backgroundColor: 'blue', size: 'medium', }
4. Check Terminal for Startup Errors
When you run npm run storybook (or yarn storybook), scan the terminal output for errors. Common culprits include:
- Missing Next.js-specific Storybook dependencies
- Syntax errors in
TestButton.jsorFaqPage.stories.js - Module resolution failures
Fix any errors you find—Storybook can fail to load stories if there's a foundational issue with your code or dependencies.
5. Refresh the Storybook Browser Tab
Sometimes Storybook doesn't auto-detect new stories immediately. Try refreshing http://localhost:6006 after making changes to your files or config.
After working through these steps, your TestButton story should appear in the Storybook sidebar under the "Button" section. If it still doesn't, double-check that all files are saved and your project structure aligns with Storybook's scanning rules.
内容的提问来源于stack exchange,提问作者Shruthi R

