There is someway to stop storybook forcing rerendering? [ANGULAR] #32785
-
SummaryHey guys, I'm having some trouble trying to test a directive input change. The main problem is that everytime i change a storybook's control, it triggers a rerender on my component, calling I'm using storybook as playground and trying to detect if my input change is calling the methods correctly, but this call rerenders and mask my expected result, that should be "only changing the input value" as angular works on runtime through dynamic fields. If im missing something, please tell me. Regards, Additional informationMy render function: render: (args: any) => ({
props: args,
template: `
<pdz-button
[pdzBadge]="pdzBadge"
[pdzBadgePosition]="pdzBadgePosition"
[pdzBadgeSize]="pdzBadgeSize"
[pdzBadgeColor]="pdzBadgeColor"
[pdzBadgeIntensity]="pdzBadgeIntensity"
>
Button Component with Badge
</pdz-button>
`,
})package.json storybook versions:
Create a reproductionNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
|
Hi @viniciusrvieira! I'm Dosu and I’m helping the storybook team. There isn't a built-in way in Storybook Angular (7.x) to prevent rerendering and ngOnInit from being called when you change controls. This is a known limitation, especially when using an inline template in your render function—each time the template changes (even if it's the same string), Storybook treats it as a new template and forces a full rerender, which re-triggers Angular lifecycle hooks like ngOnInit see details. If you want to minimize rerenders, try using a component reference (not an inline template) in your story definition. For example: export default {
component: PdzButtonComponent,
// ...other meta
};
export const Basic = {
args: { /* your inputs */ },
};This approach lets Storybook update inputs via props without recreating the component, so ngOnInit won't be called again unless the component or its module metadata changes reference. If you need to use a custom template, make sure it's defined outside the render function so it doesn't get re-instantiated on every render. However, even with these workarounds, some rerendering is unavoidable due to how Storybook manages Angular stories. There are some patterns for syncing component state with controls using decorators and wrapper components, but these don't prevent rerendering—they just help keep state in sync discussion. In summary: this is a recognized limitation, and the best you can do is avoid inline templates in the render function and use component references where possible. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
The problem i was having, was that i was debugging at "docs" page, using the
<Primary>component.Now, i perceived that the correct place to do this is inside each story, this keeps the state and doesn't force rerender.