简体   繁体   English

故事书:故事渲染完成后的回调

[英]Storybook: callback when a story has been rendered

I'm working with Storybook for components creation in pure HTML, CSS and JS (jQuery actually).我正在使用 Storybook 在纯 HTML、CSS 和 JS(实际上是 jQuery)中创建组件。

I'm wondering if there's a callback function when a story is rendered on the screen (like useEffect for React) because I need to run then the relative jQuery code only when the component is really in the DOM.我想知道在屏幕上呈现故事时是否有回调 function(例如 React 的useEffect ),因为只有当组件确实在 DOM 中时我才需要运行相关的 jQuery 代码。

Here's my situation:这是我的情况:

// Button.stories.js

// my pure HTML component
const Button = () => `<button>My button </button>`;


export default {
    title: 'Buttons',
};

export const ButtonStory = () => Button()

// would be great something like: ButtonStory.callback = function(){ do this}

There's something without workaround?有没有解决方法的东西? (Like wrap the HTML component inside react component and use useEffect for trigger the code) (比如将 HTML 组件包装在 react 组件中并使用 useEffect 触发代码)

Thanks谢谢

I'll share a not optimal solution I've found but maybe could be useful for others:我将分享一个我发现的不是最佳的解决方案,但可能对其他人有用:

MyStory.stories.js我的故事.stories.js

import {ActionBar} from '../public/components/ActionBar/actionbar.module';
import controller from "!raw-loader!../public/components/ActionBar/controller.js";
import customRenderStory from "../utils/customRenderStory";

export default {
    title: 'Basic/ActionBar',
};

//
export const ActionBarToExport= () =>
    customRenderStory(
        ActionBar(),
        controller,
        2000
    );

customRenderStory.js customRenderStory.js

export default (component, javascript, timeout) => {
    if (javascript !== undefined) setTimeout(() => eval(javascript), timeout)
    return component;
}

In this way, I can execute the code inside controller.js each time the story is rendered.这样我每次渲染故事的时候都可以执行controller.js里面的代码。 I need a timeout (which can be configured) because I can't be sure that the component will be mounted after the code execution.我需要一个超时时间(可以配置),因为我不能确定代码执行后组件是否会被挂载。

In my case, StoryBook in plain HTML and jQuery, seems is working.就我而言,普通 HTML 和 jQuery 中的 StoryBook 似乎正在运行。

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

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