-
Notifications
You must be signed in to change notification settings - Fork 280
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
fix: do not clear dialog state immediately on useDialog unmount #2584
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { StrictMode } from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import App from './App.tsx'; | ||
import './index.scss'; | ||
|
||
ReactDOM.createRoot(document.getElementById('root')!).render(<App />); | ||
ReactDOM.createRoot(document.getElementById('root')!).render( | ||
<StrictMode> | ||
<App /> | ||
</StrictMode>, | ||
); |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -257,7 +257,7 @@ | |
"react-dom": "^18.1.0", | ||
"react-test-renderer": "^18.1.0", | ||
"semantic-release": "^19.0.5", | ||
"stream-chat": "^8.46.1", | ||
"stream-chat": "^8.47.1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Additionally, bump |
||
"ts-jest": "^29.1.4", | ||
"typescript": "^5.4.5" | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
id: DialogId; | ||
isOpen: boolean | undefined; | ||
open: (zIndex?: number) => void; | ||
removalTimeout: NodeJS.Timeout | undefined; | ||
remove: () => void; | ||
toggle: (closeAll?: boolean) => void; | ||
}; | ||
|
@@ -64,6 +65,7 @@ | |
open: () => { | ||
this.open({ id }); | ||
}, | ||
removalTimeout: undefined, | ||
remove: () => { | ||
this.remove(id); | ||
}, | ||
|
@@ -76,6 +78,23 @@ | |
...{ dialogsById: { ...current.dialogsById, [id]: dialog } }, | ||
})); | ||
} | ||
|
||
if (dialog.removalTimeout) { | ||
clearTimeout(dialog.removalTimeout); | ||
this.state.next((current) => ({ | ||
...current, | ||
...{ | ||
dialogsById: { | ||
...current.dialogsById, | ||
[id]: { | ||
...dialog, | ||
removalTimeout: undefined, | ||
}, | ||
}, | ||
}, | ||
})); | ||
} | ||
|
||
return dialog; | ||
} | ||
|
||
|
@@ -117,6 +136,10 @@ | |
const dialog = state.dialogsById[id]; | ||
if (!dialog) return; | ||
|
||
if (dialog.removalTimeout) { | ||
clearTimeout(dialog.removalTimeout); | ||
} | ||
|
||
this.state.next((current) => { | ||
const newDialogs = { ...current.dialogsById }; | ||
delete newDialogs[id]; | ||
|
@@ -126,4 +149,30 @@ | |
}; | ||
}); | ||
} | ||
|
||
/** | ||
* Marks the dialog state as unused. If the dialog id is referenced again quickly, | ||
* the state will not be removed. Otherwise, the state will be removed after | ||
* a short timeout. | ||
*/ | ||
markForRemoval(id: DialogId) { | ||
const dialog = this.state.getLatestValue().dialogsById[id]; | ||
|
||
if (!dialog) { | ||
return; | ||
} | ||
|
||
this.state.next((current) => ({ | ||
...current, | ||
dialogsById: { | ||
...current.dialogsById, | ||
[id]: { | ||
...dialog, | ||
removalTimeout: setTimeout(() => { | ||
this.remove(id); | ||
}, 16), | ||
Comment on lines
+171
to
+173
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🍒 |
||
}, | ||
}, | ||
})); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,11 @@ export const useDialog = ({ id }: GetOrCreateDialogParams) => { | |
|
||
useEffect( | ||
() => () => { | ||
dialogManager.remove(id); | ||
// Since this cleanup can run even if the component is still mounted | ||
// and dialog id is unchanged (e.g. in <StrictMode />), it's safer to | ||
// mark state as unused and only remove it after a timeout, rather than | ||
// to remove it immediately. | ||
dialogManager.markForRemoval(id); | ||
Comment on lines
-12
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🍓 |
||
}, | ||
[dialogManager, id], | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's good to run demo apps in
<StrictMode />
:)