Make sure DOM is ready for Portal
This commit is contained in:
parent
21a525ba8d
commit
04e5e06d26
2 changed files with 39 additions and 19 deletions
|
@ -1,4 +1,4 @@
|
|||
import React from 'react';
|
||||
import React, { useLayoutEffect, useRef } from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
|
||||
interface IPortal {
|
||||
|
@ -8,9 +8,27 @@ interface IPortal {
|
|||
/**
|
||||
* Portal
|
||||
*/
|
||||
const Portal: React.FC<IPortal> = ({ children }) => ReactDOM.createPortal(
|
||||
children,
|
||||
document.querySelector('#soapbox') as HTMLDivElement,
|
||||
);
|
||||
const Portal: React.FC<IPortal> = ({ children }) => {
|
||||
const isRendered = useRef<boolean>(false);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (isRendered.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
isRendered.current = true;
|
||||
}, [isRendered.current]);
|
||||
|
||||
if (!isRendered.current) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
ReactDOM.createPortal(
|
||||
children,
|
||||
document.getElementById('soapbox') as HTMLDivElement,
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
export default Portal;
|
|
@ -51,21 +51,23 @@ const TestApp: FC<any> = ({ children, storeProps, routerProps = {} }) => {
|
|||
};
|
||||
|
||||
return (
|
||||
<Provider store={props.store}>
|
||||
<MemoryRouter {...routerProps}>
|
||||
<StatProvider>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ChatProvider>
|
||||
<IntlProvider locale={props.locale}>
|
||||
{children}
|
||||
<div id='soapbox'>
|
||||
<Provider store={props.store}>
|
||||
<MemoryRouter {...routerProps}>
|
||||
<StatProvider>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ChatProvider>
|
||||
<IntlProvider locale={props.locale}>
|
||||
{children}
|
||||
|
||||
<Toaster />
|
||||
</IntlProvider>
|
||||
</ChatProvider>
|
||||
</QueryClientProvider>
|
||||
</StatProvider>
|
||||
</MemoryRouter>
|
||||
</Provider>
|
||||
<Toaster />
|
||||
</IntlProvider>
|
||||
</ChatProvider>
|
||||
</QueryClientProvider>
|
||||
</StatProvider>
|
||||
</MemoryRouter>
|
||||
</Provider>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue