From c95a0f9896aa2bd2d59d5d99a856c34ee8fd461b Mon Sep 17 00:00:00 2001 From: Gilad Gray Date: Fri, 18 May 2018 14:16:49 -0700 Subject: [PATCH] Toaster.create errors if ReactDOM.render returns null --- packages/core/src/common/errors.ts | 4 ++++ packages/core/src/components/toast/toast.md | 5 +---- packages/core/src/components/toast/toaster.tsx | 8 ++++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/core/src/common/errors.ts b/packages/core/src/common/errors.ts index 33a292af86..aae6fe8b34 100644 --- a/packages/core/src/common/errors.ts +++ b/packages/core/src/common/errors.ts @@ -70,6 +70,10 @@ export const TABS_WARN_DEPRECATED = ` is deprecated since v1.11.0; consider upgrading to .` + " https://blueprintjs.com/#components.tabs.js"; +export const TOASTER_CREATE_NULL = + ns + + ` Toaster.create() is not supported inside React lifecycle methods in React 16.` + + ` See usage example on the docs site.`; export const TOASTER_WARN_INLINE = ns + ` Toaster.create() ignores inline prop as it always creates a new element.`; export const TOASTER_WARN_LEFT_RIGHT = ns + ` Toaster does not support LEFT or RIGHT positions.`; diff --git a/packages/core/src/components/toast/toast.md b/packages/core/src/components/toast/toast.md index 6891192ae5..5a8c9758af 100644 --- a/packages/core/src/components/toast/toast.md +++ b/packages/core/src/components/toast/toast.md @@ -104,15 +104,12 @@ import { Button, Position, Toaster } from "@blueprintjs/core"; class MyComponent extends React.Component<{}, {}> { private toaster: Toaster; - private refHandlers = { - toaster: (ref: Toaster) => this.toaster = ref, - }; public render() { return (
) } diff --git a/packages/core/src/components/toast/toaster.tsx b/packages/core/src/components/toast/toaster.tsx index 1b8052486b..e196be707c 100644 --- a/packages/core/src/components/toast/toaster.tsx +++ b/packages/core/src/components/toast/toaster.tsx @@ -11,7 +11,7 @@ import * as ReactDOM from "react-dom"; import { AbstractComponent } from "../../common/abstractComponent"; import * as Classes from "../../common/classes"; -import { TOASTER_WARN_INLINE, TOASTER_WARN_LEFT_RIGHT } from "../../common/errors"; +import { TOASTER_CREATE_NULL, TOASTER_WARN_INLINE, TOASTER_WARN_LEFT_RIGHT } from "../../common/errors"; import { ESCAPE } from "../../common/keys"; import { Position } from "../../common/position"; import { IProps } from "../../common/props"; @@ -97,7 +97,11 @@ export class Toaster extends AbstractComponent imp } const containerElement = document.createElement("div"); container.appendChild(containerElement); - return ReactDOM.render(, containerElement) as Toaster; + const toaster = ReactDOM.render(, containerElement) as Toaster; + if (toaster == null) { + throw new Error(TOASTER_CREATE_NULL); + } + return toaster; } public state = {