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 input as content not updating state until dialog is closed #42

Closed
federicofrontera opened this issue Nov 30, 2021 · 3 comments

Comments

@federicofrontera
Copy link

I have a confirmation dialog where I need the user to input an optional text value before confirming. My use of confirm looks something like this:

confirm( {content: <TextField defaultValue="" onChange=((e) => setValue(e.target.value)})
.then(() => APICall(value))
.catch(() => {})

And the issue is the value is not actually updated until the dialog is closed, so the value being used is always the previous one.

I'm not sure if there's something I'm doing wrong or maybe this tool is meant for simpler dialogs where it's only a description and an confirm/cancel button.

Thanks for your time.

@jonatanklosko
Copy link
Owner

Sorry for the late reply, I missed this. If that's still relevant, you're adding the then callback immediately, so the current value is captured by the () => APICall(value) closure. You could probably work around this by using useRef() and always referring to the latest value in then :)

@Bonfims
Copy link

Bonfims commented Sep 13, 2023

im using useRef but current still with undefined when then is called.

I guess that when then is called, my input field component no longer exists and their ref value will be null...

@jonatanklosko
Copy link
Owner

Hm, this is surprising, but for inputs you may want to use a more flexible approach. See #69 (comment).

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

3 participants