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

Fix ThemeProvider bug of remounting and losing state #2390

Merged
merged 9 commits into from
Jan 8, 2025
5 changes: 5 additions & 0 deletions .changeset/afraid-ducks-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@itwin/itwinui-react': patch
---

Fixed `ThemeProvider` bug of re-mounting its children and losing state when `portalContainer` is toggled between `undefined` and defined.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 12 additions & 5 deletions packages/itwinui-react/src/core/Toast/Toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,21 @@ export const ToastProvider = ({
},
});

const toasterDispatchContext = React.useContext(ToasterDispatchContext);
const toasterStateContext = React.useContext(ToasterStateContext);

// Re-use existing ToastProvider if found
if (React.useContext(ToasterStateContext) && inherit) {
return children;
}
const shouldReuse = React.useContext(ToasterStateContext) && inherit;
r100-stack marked this conversation as resolved.
Show resolved Hide resolved
const toasterDispatchContextValue = shouldReuse
? toasterDispatchContext
: dispatch;
const toasterStateContextValue = shouldReuse
? toasterStateContext
: toasterState;

return (
<ToasterDispatchContext.Provider value={dispatch}>
<ToasterStateContext.Provider value={toasterState}>
<ToasterDispatchContext.Provider value={toasterDispatchContextValue}>
<ToasterStateContext.Provider value={toasterStateContextValue}>
{children}
</ToasterStateContext.Provider>
</ToasterDispatchContext.Provider>
Expand Down
Loading