故事书在收到MSW回复后重新读者的故事

发布于 2025-02-10 03:25:41 字数 3523 浏览 2 评论 0 原文

当我在故事书故事中触发后端的请求时,该故事在收到模拟服务工作者(MSW)的回应后重新渲染。如何防止重新渲染的发生?

这是一个故事:

我遵循为我的故事书故事设置MSW。

因此,我:

  1. 安装 MSW MSW-Storybook-addon
  2. 生成的服务工作者: npx MSW INT static/
  3. 更新了我的 preview.js 调用 initialize()函数,添加了 mswdecorator ,并设置一些全局处理程序(例如, for “/api/cosites” )

在故事书中打开一个组件时,该组件包含“/api/cansuster”的请求以下情况:

  1. 最初MSW告诉我它已启用: [MSW]启用了启用。强>
  2. 我单击一个按钮以手动将请求发送到“/api/usterces”
  3. MSW告诉我此API请求已涵盖: [MSW] 15:21:20 GET/API /custerution/(200 ok)
  4. 我在控制台上打印请求结果 - 可行,我可以看到预期的结果打印出来,
  5. 但此后这个故事是无意间重新渲染的 - 这就是我想要抑制的。

FF告诉我:

与http:// localhost的连接:6006/__ webpack_hmr中断 页面加载时。

Chrome不给我这些信息。

这是设置:

package.json:

{
    ...
    "msw": {
        "workerDirectory": "static"
    }
}

main.js(Storybook):

// imports ...    
const toPath = (filePath) => path.join(process.cwd(), filePath);

module.exports = {
  core: {
    builder: "webpack5",
  },
  stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
  addons: [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
  ],
  features: {
    emotionAlias: false,
  },
  webpackFinal: (config) => {
    return {
      ...config,
      module: {
        ...config.module,
        rules: custom.module.rules, // rules how to handle file extensions
      },
      resolve: {
        ...config.resolve,
        alias: {
          ...config.resolve.alias,
          "@emotion/core": toPath("node_modules/@emotion/react"),
          "emotion-theming": toPath("node_modules/@emotion/react"),
        },
      },
    };
  },
  staticDirs: ["../path/to/static/"], // that's our public
};

以下是一些代码:

preview.js

export const parameters =  {
    ...
    msw: {
      handlers: [
        rest.get("/api/customers", (req, res, ctx) => {
          let customers = ...;
        
          return res(
            ctx.json({
              data: customers,
            })
          );
        }),
      ],
    },
  }

exkplestory.stories。 JSX:

export default {
  title: "Customers",
};

const getCustomers = () => {
    // ... set axios config etc.
    axios.get("/api/customers", config)
        .then((response) => response.data.data)
        .then((customers) => console.log(customers)); // yep, the customers gets printed!
};

export const ExampleStory = () => {
  return (
    <div>
      Simple demo story
      <button onClick={getCustomers}>Get customers</button>
    </div>
  );
};

节点模块:

  • email-potection” class =“ __ cf_email__” data-cfemail =“ F39E8084B3C3DDDC7C1DDC0
  • [emagy&nbsp;

When I trigger requests to the backend within a Storybook story, the story is re-rendered after receiving the response from Mock Service Worker (MSW). How can I prevent the re-rendering from happening?

Here's the story:

I followed the tutorial on https://storybook.js.org/addons/msw-storybook-addon to set up MSW for my Storybook stories.

So I:

  1. installed msw and msw-storybook-addon
  2. generated a service worker: npx msw init static/
  3. updated my preview.js to call the initialize() function, added the mswDecorator, and set some global handlers (e. g. for "/api/customers")

When opening a component in Storybook that includes a request to "/api/customers" the following happens:

  1. Initially MSW is telling me that it's enabled: [MSW] Mocking enabled.
  2. I click a button to manually send a request to "/api/customers"
  3. MSW is telling me that this API request is covered: [MSW] 15:21:20 GET /api/customers/ (200 OK)
  4. I print the request results on the console - that works and I can see the expected results printed
  5. But right after that the story is unintentionally re-rendered - this is what I want to suppress.

FF is telling me:

The connection to http://localhost:6006/__webpack_hmr was interrupted
while the page was loading.

Chrome doesn't give me that piece of information.

Here's the setup:

package.json:

{
    ...
    "msw": {
        "workerDirectory": "static"
    }
}

main.js (Storybook):

// imports ...    
const toPath = (filePath) => path.join(process.cwd(), filePath);

module.exports = {
  core: {
    builder: "webpack5",
  },
  stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
  addons: [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
  ],
  features: {
    emotionAlias: false,
  },
  webpackFinal: (config) => {
    return {
      ...config,
      module: {
        ...config.module,
        rules: custom.module.rules, // rules how to handle file extensions
      },
      resolve: {
        ...config.resolve,
        alias: {
          ...config.resolve.alias,
          "@emotion/core": toPath("node_modules/@emotion/react"),
          "emotion-theming": toPath("node_modules/@emotion/react"),
        },
      },
    };
  },
  staticDirs: ["../path/to/static/"], // that's our public
};

Here's some code:

preview.js

export const parameters =  {
    ...
    msw: {
      handlers: [
        rest.get("/api/customers", (req, res, ctx) => {
          let customers = ...;
        
          return res(
            ctx.json({
              data: customers,
            })
          );
        }),
      ],
    },
  }

ExampleStory.stories.jsx:

export default {
  title: "Customers",
};

const getCustomers = () => {
    // ... set axios config etc.
    axios.get("/api/customers", config)
        .then((response) => response.data.data)
        .then((customers) => console.log(customers)); // yep, the customers gets printed!
};

export const ExampleStory = () => {
  return (
    <div>
      Simple demo story
      <button onClick={getCustomers}>Get customers</button>
    </div>
  );
};

Node Modules:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文