可复用的 React 提示组件

huangapple go评论58阅读模式
英文:

Reusbale React Toastify Component

问题

A react-toast组件创建的目的是将其发布为私有npm包。它对许多内部项目都很有用。

以下是我的代码:https://codesandbox.io/s/custom-toastify-toast-with-react-component-forked-mu2l9c?file=/src/App.js:297-484

但是遇到了两个问题:

  1. 单击按钮时,会显示两个toast框。
  2. 向子组件传递引用。

此外,代码中使用了refcurrent.showToast。因此,将refs传递给子组件似乎不太好。

我想要实现的是一次声明,随处使用。

因此,在父组件中:

import {ToastSuccess, ToastSuccessMeta, ToastError, ToastErrorMeta } from 'custom-library'

return(
<>
   <ToastSuccess />
   <ToastError />
</>
)

在子组件中:

<button  onClick={() => ToastSuccessMeta.showToast({ position: "top-right", message: "Custom Success Toast" })}>Show Toast </button>
<button  onClick={() => ToastErrorMeta.showToast({ position: "top-right", message: "Custom Error Toast" })}>Show Toast Error</button>

我不知道是否可以实现这一点。请注意,custom-library是Toast-Container将被配置的地方。项目将安装并导入它以使用toast。

感谢您的帮助。

谢谢

英文:

A react-toast component creadted with an intention to publish it as an private npm pacakge. It can usedul for many internal projects.

So, below is my code: https://codesandbox.io/s/custom-toastify-toast-with-react-component-forked-mu2l9c?file=/src/App.js:297-484

However there are two problems encountered

  1. on Click of buttons, two toast box are being displayed
  2. Passing down of Ref to child components.

Again, the code uses, ref, current.showToast. So it sounds bad to pass down refs to child Components.

What I want to achieve is DECLARE ONE TIME and USE ANYWHERE.

So, in the parent Component

import {ToastSuccess, ToastSuccessMeta, ToastError, ToastErrorMeta } from &#39;custom-library&#39;

return(
&lt;&gt;
   &lt;ToastSuccess /&gt;
   &lt;ToastError /&gt;
&lt;/&gt;
)
   

In Child components

 &lt;button  onClick={() =&gt; ToastSuccessMeta.showToast({ position: &quot;top-right&quot;, message: &quot;Custom Success Toast&quot; })}&gt;Show Toast &lt;/button&gt;
 &lt;button  onClick={() =&gt; ToastErrorMeta.showToast({ position: &quot;top-right&quot;, message: &quot;Custom Error Toast&quot; })}&gt;Show Toast Erro&lt;/button&gt;

I don't know if I can achieve this. Please note that custom-library is the place where Toast-Container will be configured. . And projects will install and import it to use the toast.

Your help is appreciated.

Thank you

答案1

得分: 1

我建议您,而不是创建多个 toast 组件,您可以创建一个通用组件,如下面的代码所示,然后您可以在应用程序的任何地方使用此组件。

import { toast } from "react-toastify";

export default (options) => {
  const toastOptions = {
    position: options.position,
    autoClose: 3000,
    hideProgressBar: false,
    closeOnClick: true,
    pauseOnHover: true,
    draggable: true,
    progress: undefined,
    theme: "colored"
  };

  switch (options.type) {
    case "info":
      toast.info(options.message, toastOptions);
      break;
    case "success":
      toast.success(options.message, toastOptions);
      break;
    case "error":
      toast.error(options.message, toastOptions);
      break;
    case "warning":
      toast.warning(options.message, toastOptions);
      break;
    default:
      toast.error("出现了一些问题", toastOptions);
  }
};

您可以在您的应用程序的任何地方使用此组件。

英文:

I recommended you, instead of making a multiple toast components you can make a common component as you can see the below code and you can use this component anywhere in your app.

import { toast } from &quot;react-toastify&quot;;

export default (options) =&gt; {
  const toastOptions = {
    position: options.position,
    autoClose: 3000,
    hideProgressBar: false,
    closeOnClick: true,
    pauseOnHover: true,
    draggable: true,
    progress: undefined,
    theme: &quot;colored&quot;
  };

  switch (options.type) {
    case &quot;info&quot;:
      toast.info(options.message, toastOptions);
      break;
    case &quot;success&quot;:
      toast.success(options.message, toastOptions);
      break;
    case &quot;error&quot;:
      toast.error(options.message, toastOptions);
      break;
    case &quot;warning&quot;:
      toast.warning(options.message, toastOptions);
      break;
    default:
      toast.error(&quot;Something went wrong&quot;, toastOptions);
  }
};

huangapple
  • 本文由 发表于 2023年4月19日 16:45:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定