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

Controlled value is undefined when adding an "onChange" callback to a "dropdown" option #505

Open
bgirschig opened this issue Jun 7, 2024 · 2 comments

Comments

@bgirschig
Copy link

Hello!
I'm experiencing an issue with a dropdown control when I try to add a callback to it:

const { dogFood } = useControls({
  dogFood: {
    options: {
      "meat": "yay",
      "chocolate": "nay",
    },
    onChange: console.log,
  },
  something: True,
});

// "dogFood" is undefined

The callback is called at the correct time, but the value is not usable.

typescript compilation fails with the following error:

Property dogFood does not exist on type { something: boolean; }"

leva: 0.9.35
tsc: 5.4.5

By the way, thanks for the lib! It's really pretty and helpful.

@zaesur

This comment was marked as resolved.

@zaesur
Copy link

zaesur commented Sep 25, 2024

In fact, this is not a bug but intended.

Looking at this documentation, we can see that we need to add transient: false whenever we want to consume a value.

By default, if you create an input with an onChange callback, the transient value is set to true, meaning that you are not subscribed to the input value and cannot consume it.

If you add transient: false the example works without a problem:

const useHook = () => {
  const { dogFood } = useControls({
    dogFood: {
      options: {
        meat: "yay",
        chocolate: "nay",
      },
      onChange: console.log,
      transient: false,
    },
    something: true,
  });

  return dogFood;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants