简体   繁体   English

故事书 Canvas:“ReferenceError:未定义反应”

[英]Storybook Canvas: 'ReferenceError: react is not defined'

Newbie to Storybook here. Storybook 的新手在这里。

I'm trying to integrate Storybook into my Gatsby front end.我正在尝试将 Storybook 集成到我的 Gatsby 前端中。 However, when trying to preview the test components in Storybook Canvas I get the following error:但是,当尝试在 Storybook Canvas 中预览测试组件时,出现以下错误:

react is not defined ReferenceError: react is not defined at react-dom/client (http://localhost:6006/main.iframe.bundle.js:1970:18) at webpack_require (http://localhost:6006/runtime~main.iframe.bundle.js:28:33) at fn (http://localhost:6006/runtime~main.iframe.bundle.js:339:21) at webpack_require .t (http://localhost:6006/runtime~main.iframe.bundle.js:106:38) react is not defined ReferenceError: react is not defined at react-dom/client (http://localhost:6006/main.iframe.bundle.js:1970:18) 在webpack_require (http://localhost:6006/runtime~ main.iframe.bundle.js:28:33) 在 fn (http://localhost:6006/runtime~main.iframe.bundle.js:339:21) 在webpack_require .t (http://localhost:6006/运行时~main.iframe.bundle.js:106:38)

在此处输入图像描述

I'm able to see the component preview in Storybook Docs but not in Storybook Canvas.我能够在 Storybook Docs 中看到组件预览,但在 Storybook Canvas 中看不到。

Link to repository: https://github.com/akarpov91/gatsby-tutorial存储库链接: https://github.com/akarpov91/gatsby-tutorial

Try adding the following snippet in your main.js :尝试在您的main.js中添加以下代码片段:

module.exports = {
  // ...
  babel: async (options) => ({
    ...options,
    presets: [
      ...options.presets,
      [
    '@babel/preset-react', {
      runtime: 'automatic',
    },
        'preset-react-jsx-transform'
      ],
    ],
  }),
};

Apparently, @storybook/react adds @babel/preset-react without runtime: 'automatic' property显然, @storybook/react添加了@babel/preset-react without runtime: 'automatic'属性

I have had the same problem, try copying this into your .storybook/main.js config.我遇到了同样的问题,请尝试将其复制到您的.storybook/main.js配置中。 Hope this works for you too.希望这对你也有用。

module.exports = {
  // You will want to change this to wherever your Stories will live
  stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
  addons: ["@storybook/addon-links", "@storybook/addon-essentials"],
  framework: "@storybook/react",
  core: {
    builder: "webpack5",
  },
  webpackFinal: async config => {
    // Transpile Gatsby module because Gatsby includes un-transpiled ES6 code.
    config.module.rules[0].exclude = [/node_modules\/(?!(gatsby)\/)/]

    // Use installed babel-loader which is v8.0-beta (which is meant to work with @babel/core@7)
    config.module.rules[0].use[0].loader = require.resolve("babel-loader")

    // Use @babel/preset-react for JSX and env (instead of staged presets)
    config.module.rules[0].use[0].options.presets = [
      require.resolve("@babel/preset-react"),
      require.resolve("@babel/preset-env"),
    ]

    config.module.rules[0].use[0].options.plugins = [
      // Use @babel/plugin-proposal-class-properties for class arrow functions
      require.resolve("@babel/plugin-proposal-class-properties"),

      // Use babel-plugin-remove-graphql-queries to remove graphql queries from components when rendering in Storybook
      // While still rendering content from useStaticQuery in development mode
      [
        require.resolve("babel-plugin-remove-graphql-queries"),
        {
          stage: config.mode === `development` ? "develop-html" : "build-html",
          staticQueryDir: "page-data/sq/d",
        },
      ],
    ]

    return config
  },
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM