React-Toastify组件可重复使用
P粉514458863
P粉514458863 2024-02-03 18:21:39
0
1
362

我正在尝试创建一个可重用的 Toast 组件。

这里是代码:https://codesandbox.io/s/custom-toastify-toast-with-react-component-forked-mu2l9c?file=/src/Toast.js:146-680

在渲染 Toast 组件本身时[下面有评论],toast 会漂亮地弹出。

return (
    <>
      {/* <Toast /> */}   ---> This one works perfectly.
      <input
        type="text"
        value={input}
        onChange={(e) => setInput(e.target.value)}
        name="input"
      />
    </>

但是,我正在尝试使用公开的 toastMeta 来实现对 toast 的调用。这样调用者只需输入 toastMeta.message("please show up..") 即可获取 Toast。还传递可选参数水平和垂直位置。

问题:无法使用 toastMeta.message("") 调用 toast 组件

注意:此 CustomToast 将是一个 npm 包,因此调用者必须安装此库并导入 toastMeta

export const toastMeta = {
  position: "top-right",
  message: "Error Toast"
};

export const Toast = ({ className, children, ...props }) => {
  return (
    <>
      <ToastContainer {...props}>
        {toast.error(toastMeta.message, {
          position: toastMeta.position,
          autoClose: 3000,
          hideProgressBar: false,
          closeOnClick: true,
          pauseOnHover: true,
          draggable: true,
          progress: undefined,
          theme: "colored"
        })}
      </ToastContainer>
    </>
  );
};

每次击键后调用 toast..

const [input, setInput] = useState("");

  useEffect(() => {
    toastMeta("Error Message..");
  }, [input]);

  return (
    <>
      {/* <Toast /> */}
      <input
        type="text"
        value={input}
        onChange={(e) => setInput(e.target.value)}
        name="input"
      />

创建 Toast 组件的原因:

用于版本控制,因为它是公共库的组件之一。公共库包含所有 UI 元素。

非常感谢您的帮助。预先感谢您。

P粉514458863
P粉514458863

全部回复(1)
P粉090087228

您不能将对象作为函数调用,这也是实现不正确的原因之一,您需要使用 toast 的 ref,然后动态传递值。

请检查代码,希望对您有所帮助!

App.js

import React, { useEffect, useRef, useState } from "react";
import { Toast, toastMeta } from "./Toast";

const App = () => {
  const [input, setInput] = useState("");

  const toastRef = useRef(null);

  useEffect(() => {
    // toastMeta("Error Message..");
    if (input !== "") {
      toastRef.current.showToast({
        position: "top-right",
        message: "Custom Error Toast"
      });
    }
  }, [input]);

  return (
    
      
       setInput(e.target.value)}
        name="input"
      />
    >
  );
};

export default App;

Toast.js

import React, { forwardRef, useImperativeHandle, useState } from "react";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";

export const toastMeta = () => {
  return { position: "top-right", message: "Error Toast" };
};

export const Toast = forwardRef(({ className, children, ...props }, ref) => {
  const [toastConfig, setToastConfig] = useState({});

  useImperativeHandle(ref, () => ({
    showToast: (_data) => {
      setToastConfig(_data);
    }
  }));

  return (
    
      {toast.error(toastConfig?.message, {
        position: toastConfig?.position,
        autoClose: 3000,
        hideProgressBar: false,
        closeOnClick: true,
        pauseOnHover: true,
        draggable: true,
        progress: undefined,
        theme: "colored"
      })}
    
  );
});
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!