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

[1.x] Toaster.create errors if ReactDOM.render returns null #2523

Merged
merged 1 commit into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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