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

Core: Fix useParameter with nullish coalescing #17327

Merged
merged 1 commit into from Feb 7, 2022

Conversation

evancharlton
Copy link
Contributor

Issue

The useParameter(..) hook accepts a key and a default value. It then
provides the default value if the current story has not provided a
desired value for the relevant parameters object.

What I did

This change updates the logic of when to fall back to the default. The
old logic used a boolean operator (||), which worked as long as the
provided value wasn't falsey. Instead, account for this by using the
nullish coalescing operator (??).

How to test

  1. Create an addon which consumes a parameter like:

    const parameters = useParameter("myAddon", { defaultValue: true });
  2. Create a story which looks like:

    export const Bug = {
      parameters: {
        myAddon: false
      }
    };
  3. Note that the value of parameters in your addon will always be
    the default object, not false (as was requested by the story).

  • Is this testable with Jest or Chromatic screenshots?
  • Does this need a new example in the kitchen sink apps?
  • Does this need an update to the documentation?

If your answer is yes to any of these, please make sure to include it in your PR.

@nx-cloud
Copy link

nx-cloud bot commented Jan 25, 2022

☁️ Nx Cloud Report

CI ran the following commands for commit 09d44bc. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

The `useParameter(..)` hook accepts a key and a default value. It then
provides the default value if the current story has not provided a
desired value for the relevant `parameters` object.

This change updates the logic of when to fall back to the default. The
old logic used a boolean operator (`||`), which worked as long as the
provided value wasn't falsey. Instead, account for this by using the
nullish coalescing operator (`??`).
@yannbf yannbf added core bug patch:yes Bugfix & documentation PR that need to be picked to main branch labels Feb 7, 2022
@shilman shilman changed the title fix: Use nullish coalescing operator in useParameter(..) Core: Fix useParameter with nullish coalescing Feb 7, 2022
Copy link
Member

@yannbf yannbf left a comment

Choose a reason for hiding this comment

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

Hey @evancharlton thank you for your contribution! Looks great 👍

@shilman shilman merged commit 4646214 into storybookjs:next Feb 7, 2022
@evancharlton evancharlton deleted the useParameter-fallback branch February 7, 2022 19:12
@shilman shilman added the patch:done Patch/release PRs already cherry-picked to main/release branch label Feb 12, 2022
shilman added a commit that referenced this pull request Feb 12, 2022
Core: Fix `useParameter` with nullish coalescing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug core patch:done Patch/release PRs already cherry-picked to main/release branch patch:yes Bugfix & documentation PR that need to be picked to main branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants