-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
Dialog does not reset internal state when closing. #16325
Comments
Closing this issue. I apologize, it looks like I did not set the |
@matthewdordal I'm running into similar issue but cannot reproduce with the sample code you provided, care to elaborate a bit more? Thanks! |
@tkforce I'm strangely running into a similar issue where the initial state of a component for a specific array prop is never reset. What's the data type that isn't being reset for your component, and how are you setting the initial state? |
My issue stemmed from the fact that I had been setting the initial state of my component as an object outside the component. This was causing the data to be stored regardless of the component unmounting. As a means of resolving this, I simply set the initialState as a property of the class using the |
Setting the |
I have been successful in using the following pattern when I need to wholesale reset the modal/dialog state. The crux of this approach is updating of the
There may be a more elegant solution that I'm not aware of, but I typically find it most convenient to nuke and pave with each close. |
i found easy solution for same pb : |
As a workaround, I have been using Formik to handle the state of any forms contained within a Dialog. This eliminates the necessity of a key for the Dialog component in the parent component. Example:
|
@seansullivan @oliviertassinari @matthewdordal @DollarAkshay @tkforce
|
I found that what i was doing wrong was putting the dialog with in its own component rather than defining with in the parent and defining the content of the dialog with in its own component.
when you think about it every thing out side of {...} does not depend on the open state and will persist regardless of a open being passed in.
|
the reasoning does not make sense but it works regardless, thanks |
It does if you look at the second example. stuff and setstuff are not with in the dialog so it will not be affected/reset when the dialog is opened or closed. But I could have said that more clearly |
hmm I see what you mean now |
I'm aware of the virtual DOM that React handles, but not in this way, I mean, if an element stores the same key, it could preserve the last state, really nice trick |
This works to me! Thanks! |
import { Dispatch, SetStateAction, useEffect, useRef, useState } from "react";
function useDialogState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>, number] {
const key = useRef<number>(0);
const [open, setOpen] = useState(initialState);
useEffect(() => {
if (!open) key.current = key.current + 1;
}, [open]);
return [open, setOpen, key.current];
}
export default useDialogState; Custom hook using @seansullivan approach. |
This issue still exists. And I don't understand the purpose of keeping mounted the dialog? If I want to keep mounted the dialog then I can use the keepMounted prop. I think this is a bug and need to fix |
Either one of this will do <Dialog key={someUniqueKey} /> or simply {open && <Dialog open /> } } |
This same behaviour is also present in angular material. |
Expected Behavior 🤔
When I open a dialog and change the state of the DialogContent the state should be reset once the dialog is closed. That way previous state is not persisted when the dialog is opened again.
Current Behavior 😯
Open a dialog that can set state in it's content. Change the state and close the dialog. Open the dialog again and the previous state is displayed in the dialog content.
Steps to Reproduce 🕹
Here is a Code Sandbox demonstrating the issue.
I have also attached a gif (hopefully) better demonstrate the issue.
Context 🔦
I have dialogs with form inputs. When a user enters text into the input, closes the dialog, and then opens the dialog again the input shows the previously entered text instead of the default state values
Your Environment 🌎
The text was updated successfully, but these errors were encountered: