Skip to content

Commit

Permalink
Toaster.create errors if ReactDOM.render returns null
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilad Gray committed May 18, 2018
1 parent 03158fc commit c95a0f9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/common/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export const TABS_WARN_DEPRECATED =
` <Tabs> is deprecated since v1.11.0; consider upgrading to <Tabs2>.` +
" 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.`;

Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/components/toast/toast.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div>
<Button onClick={this.addToast} text="Procure toast" />
<Toaster position={Position.TOP_RIGHT} ref={this.refHandlers.toaster} />
<Toaster position={Position.TOP_RIGHT} ref={ref => this.toaster = ref} />
</div>
)
}
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/components/toast/toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -97,7 +97,11 @@ export class Toaster extends AbstractComponent<IToasterProps, IToasterState> imp
}
const containerElement = document.createElement("div");
container.appendChild(containerElement);
return ReactDOM.render(<Toaster {...props} inline={true} />, containerElement) as Toaster;
const toaster = ReactDOM.render(<Toaster {...props} inline={true} />, containerElement) as Toaster;
if (toaster == null) {
throw new Error(TOASTER_CREATE_NULL);
}
return toaster;
}

public state = {
Expand Down

1 comment on commit c95a0f9

@blueprint-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Toaster.create errors if ReactDOM.render returns null

Preview: documentation | landing | table

Please sign in to comment.