📜  显示 react-toastify 的自定义 toast 函数 - Toast show - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:02.195000             🧑  作者: Mango

代码示例1
import { toast } from "react-toastify";
const defaultPosition = toast.POSITION.BOTTOM_CENTER;

/**
 * Show Toast
 *
 * Display toast
 *
 * @param {string} type message type, success/error
 * @param {string} msg toast message
 * @param {integer} autoClose auto close value in millisecond
 * @param {string} className toaster class name
 * @param {string} position toast position; ex-'top-right', 'top-center', 'top-left', 'bottom-right', 'bottom-center', 'bottom-left'
 */

export const showToast = ( type = "success", msg, autoClose = 2000, className = "primaryColor", position = defaultPosition ) => {
  if (type === "success") {
    toast.success(msg, {
      autoClose: autoClose === null ? 2000 : autoClose,
      className: className === null ? "primaryColor" : className,
      position: position,
    });
  } else if (type === "error") {
    toast.error(msg, {
      autoClose: autoClose === null ? 2000 : autoClose,
      className: className === null ? "dangerColor" : className,
      position: position,
    });
  }
};


// How to use react-toastify
showToast('success', 'Product add to cart successfully !');

showToast('error', 'Please fill the inputs properly.');