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

Slider component doesn't have updated state value #2840

Closed
IDrumsey opened this issue Oct 29, 2022 · 5 comments
Closed

Slider component doesn't have updated state value #2840

IDrumsey opened this issue Oct 29, 2022 · 5 comments

Comments

@IDrumsey
Copy link

What package has an issue

@mantine/core

Describe the bug

Using version @mantine/core@5.6.2

I have a state variable that I am updating with a typical button press onClick event handler. When I console the state value from a slider's onChange event handler, it doesn't print the updated value, but a Button component does print the updated value.

In the sandbox, click the first button to update the state. Click the second button and view the console to see it's printing the correct value. Then click the slider to see that it's printing the old value.

What version of @mantine/hooks page do you have in package.json?

5.6.2

If possible, please include a link to a codesandbox with the reproduced problem

https://codesandbox.io/s/charming-lena-2f2hly?file=/src/App.js

Do you know how to fix the issue

No

Are you willing to participate in fixing this issue and create a pull request with the fix

No

Possible fix

No response

@rtivital
Copy link
Member

It is not planned to fix this issue soon, I can only advise not to use state variables in Slider onChange method

@tagdara
Copy link

tagdara commented Oct 29, 2022

Does this mean the controlled example in the docs is also broken? https://mantine.dev/core/slider/#controlled
Surely this is a very normal use case - is there a workaround?

I rolled back to 5.5.6, which loses a lot of your other nice work, but I have sliders in a lot of places to control volume, and they need to be controlled in order for other sources to update them.

@rtivital
Copy link
Member

Nope, these are two separate issues, see this PR to learn more about the issue that was introduced recently #2839

@cyantree
Copy link
Contributor

The issue is quite old but because the underlying mechanism hasn't changed here are two approaches for dealing with them:

  • Manage the state variable also with useRef() to always access its current value
  • Manage your callback via useRef() and call it within your handler

Both approaches can be seen here:
https://codesandbox.io/s/sad-matan-43d005?file=/src/App.js

@rtivital Would this be a nice addition for @mantine/hooks? I use it quite frequently to work around such issues and also reduce the need for rerenderings.

import { useRef } from 'react';

export function useLiveRef<T>(value: T) {
    const ref = useRef(value);
    ref.current = value;

    return ref;
}

@FinnRG
Copy link

FinnRG commented Jul 11, 2023

Was this resolved by #4497?

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

5 participants