diff --git a/components/doc/toast/index.js b/components/doc/toast/index.js
index 464dee8bec..8d99ab81c0 100644
--- a/components/doc/toast/index.js
+++ b/components/doc/toast/index.js
@@ -711,7 +711,16 @@ toast.current.show({life: 5000, severity: 'error', summary: 'Error Message', det
{`
- toast.current.clear();
+toast.current.clear();
+`}
+
+
+
Replacing Messages
+ replace(newMessages) method adds new messages after removing all old messages.
+
+
+{`
+toast.current.replace(newMessages);
`}
diff --git a/components/lib/messages/Messages.js b/components/lib/messages/Messages.js
index e17bc93dfb..d0e0a567b0 100644
--- a/components/lib/messages/Messages.js
+++ b/components/lib/messages/Messages.js
@@ -11,7 +11,7 @@ export const Messages = React.memo(React.forwardRef((props, ref) => {
const show = (value) => {
if (value) {
- let messages = [];
+ let messages;
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
@@ -33,7 +33,8 @@ export const Messages = React.memo(React.forwardRef((props, ref) => {
}
const replace = (value) => {
- setMessagesState(value);
+ const replaced = Array.isArray(value) ? value : [value];
+ setMessagesState(replaced);
}
const onClose = (message) => {
diff --git a/components/lib/toast/Toast.d.ts b/components/lib/toast/Toast.d.ts
index 6cee2c0bd0..0e241354eb 100644
--- a/components/lib/toast/Toast.d.ts
+++ b/components/lib/toast/Toast.d.ts
@@ -41,4 +41,5 @@ export interface ToastProps {
export declare class Toast extends React.Component {
public show(message: ToastMessageType): void;
public clear(): void;
+ public replace(message: ToastMessageType): void;
}
diff --git a/components/lib/toast/Toast.js b/components/lib/toast/Toast.js
index f2936853cb..b0b1abb002 100644
--- a/components/lib/toast/Toast.js
+++ b/components/lib/toast/Toast.js
@@ -18,18 +18,14 @@ export const Toast = React.memo(React.forwardRef((props, ref) => {
let messages;
if (Array.isArray(value)) {
- const currentMessages = [...value];
- for (let i = 0; i < currentMessages.length; i++) {
- const message = { ...currentMessages[i] };
- message.id = messageIdx++;
- currentMessages[i] = message;
+ for (let i = 0; i < value.length; i++) {
+ value[i].id = messageIdx++;
+ messages = [...messagesState, ...value];
}
- messages = [...messagesState, ...currentMessages];
}
else {
- const currentMessage = { ...value };
- currentMessage.id = messageIdx++;
- messages = messagesState ? [...messagesState, currentMessage] : [currentMessage];
+ value.id = messageIdx++;
+ messages = messagesState ? [...messagesState, value] : [value];
}
messagesState.length === 0 && ZIndexUtils.set('toast', containerRef.current, PrimeReact.autoZIndex, props.baseZIndex || PrimeReact.zIndex['toast']);
@@ -43,6 +39,11 @@ export const Toast = React.memo(React.forwardRef((props, ref) => {
setMessagesState([]);
}
+ const replace = (value) => {
+ const replaced = Array.isArray(value) ? value : [value];
+ setMessagesState(replaced);
+ }
+
const onClose = (message) => {
const messages = messagesState.filter(msg => msg.id !== message.id);
setMessagesState(messages);
@@ -66,6 +67,7 @@ export const Toast = React.memo(React.forwardRef((props, ref) => {
React.useImperativeHandle(ref, () => ({
show,
+ replace,
clear
}));