Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manager API: Fix api.getAddonStatedefault value #23804

Merged
merged 4 commits into from
Aug 23, 2023

Conversation

sookmax
Copy link
Contributor

@sookmax sookmax commented Aug 11, 2023

Closes #23741 #23787

What I did

I've noticed api.getAddonState() sometimes (not sure the exact condition) returns undefined, which affects consumers of api.setAddonState().

More specifically, #23787 occurs when the state updater function setCount() in code/addon/actions/src/manager.tsx attempts to access c.count assuming the state { count: number; } is available all the time (i.e., setCount((c) => ({ ...c, count: c.count + 1 }));). Unfortunately as I mentioned above, sometimes api.getAddonState(), which is the value of the parameter c of the callback in setCount(), results in undefined.

This PR fixes the issue by providing defaultState when c is undefined in useSharedState().

How to test

Checklist

  • Make sure your changes are tested (stories and/or unit, integration, or end-to-end tests)
  • Make sure to add/update documentation regarding your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli/src/sandbox-templates.ts
  • Make sure this PR contains one of the labels below.

["cleanup", "BREAKING CHANGE", "feature request", "bug", "build", "documentation", "maintenance", "dependencies", "other"]

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>

@ndelangen
Copy link
Member

The confusion comes from a race condition:

When using useAddonState() in 2 places in the react tree, or even across manager & preview... then if both do not have the exact same default config there's the possibility of creating an infinite loop.

So it's recommended to only have the default state value defined in 1 place. everywhere else you might get an undefined initially, but it will get called again.

@sookmax
Copy link
Contributor Author

sookmax commented Aug 11, 2023

Oh I see.. so it's more involved than I originally thought.

But my fix merely uses the default state provided to useSharedState, which was already there before the fix, and even the initial undefined passed here: setCount((c) => ({ ...c, count: c.count + 1 })) will produce a runtime error and the counter becomes out of sync with the number of items shown in the action panel.

Copy link
Contributor

@kasperpeulen kasperpeulen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comitted on this one, so I can not approve anymore 😅

code/lib/manager-api/src/index.tsx Outdated Show resolved Hide resolved
@ndelangen ndelangen changed the title Fix: use defaultState when api.getAddonState returns undefined. Manager API: Fix defaultState when api.getAddonState returns undefined. Aug 23, 2023
@ndelangen ndelangen changed the title Manager API: Fix defaultState when api.getAddonState returns undefined. Manager API: Fix api.getAddonStatedefault value Aug 23, 2023
@kasperpeulen kasperpeulen merged commit 1c465ce into storybookjs:next Aug 23, 2023
9 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Since v7.2.0: Actions tab doesn't show count anymore + console error
4 participants