-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Toaster.create() uses ReactDOM.render which is being deprecated in React 18 #5212
Comments
Can you work around this by using one of the alternative Toaster APIs? |
I've been able to work this around by replacing this: export const toaster = Toaster.create(); with this: export let toaster: Toaster;
createRoot(document.getElementById('toaster')!).render(
<Toaster
ref={instance => {
toaster = instance!;
}}
/>,
); and manually adding DOM element to the body: <body>
<div id="root"></div>
<div id="toaster"></div> <!-- <--- added this -->
</body> |
Is there any date for getting a fix to this one ? Im thinking of ways to get around this but I don't come with any solution. We were using I can use one of the alternative methods to get a reference to a
The issue with doing this is that I cannot test toasts are shown in integration tests unless I bring this very specific component. Is there any solution that Im missing ? |
Any updates on this? |
@xtecox updates on what, exactly? There are some alternative APIs mentioned above that should allow you to work around the |
I have created a small module, hope it helps. You can install it from @hcfy/create-toaster, or use the following code in your project import { IToasterProps, Toaster } from '@blueprintjs/core'
import { createRoot } from 'react-dom/client'
export default function createToaster(
props?: IToasterProps,
container = document.body
) {
const containerElement = document.createElement('div')
container.appendChild(containerElement)
const root = createRoot(containerElement)
return new Promise<Toaster>((resolve, reject) => {
root.render(
<Toaster
{...props}
usePortal={false}
ref={(instance) => {
if (!instance) {
reject(new Error('[Blueprint] Unable to create toaster.'))
} else {
resolve(instance)
}
}}
/>
)
})
} Before: const toaster = Toaster.create(props, container) After: const toaster = await createToaster(props, container) |
Adding a Toaster component somewhere on the page like this
|
Hi folks — We've merged a new The next version isn't published yet. As a preview for when it is published, the new import { OverlayToaster } from "@blueprintjs/core";
import { createRoot } from "react-dom/client";
const toaster = await OverlayToaster.createAsync(/* props */ {}, {
domRenderer: (toaster, containerElement) => createRoot(containerElement).render(toaster),
});
toaster.show({ message: "Hello React 18!" }) At some point in the future, Blueprint will require React 18 and drop support for React 16 and 17. When this happens, the DetailsTo account for changes in React 18, the new Why is the new API asynchronous?The change to Blueprint's API reflects a change in React's API. The new import * as React from "react";
import { createRoot } from "react-dom/client";
const toaster = React.createRef<OverlayToaster>();
createRoot(containerElement)
.render(<OverlayToaster {...props} ref={toaster} usePortal={false} />)
// OverlayToaster render function is not yet executed.
// ‼️ This is null ‼️
ref.current
setTimeout(() => {
// Okay, now the OverlayToaster render() function has ran.
ref.current // now available
}, 0) This is different than import * as React from "react";
import * as ReactDOM from "react-dom";
const toaster = React.createRef<OverlayToaster>();
ReactDOM.render(
<OverlayToaster {...props} ref={toaster} usePortal={false} />,
containerElement
);
// OverlayToaster render function has ran by this point.
ref.current // instance of <OverlayToaster /> This seems to be an intentional change in React 18. See the “What about the render callback?” section under Replacing render with createRoot. Why do I need to pass in a custom
|
Environment
https://github.com/palantir/blueprint/blob/develop/packages/core/src/components/toast/toaster.tsx#L130
In console:
Actual behavior
React warns us against using ReactDOM.render
Expected behavior
Blueprint should not use ReactDOM.render for a toast
Possible solution
Relevant thread:
chakra-ui/chakra-ui#5795 (comment)
The text was updated successfully, but these errors were encountered: