【问题标题】:how do i make to appear the react toastify without using onClick button? it should appear once the page loads我如何在不使用 onClick 按钮的情况下显示反应烤面包?它应该在页面加载后出现
【发布时间】:2022-01-03 07:07:34
【问题描述】:

我展示的代码是一个带有 onClick 按钮和自定义自动关闭设置时间为 1 分钟的 react toastify 库。我想在不使用按钮的情况下使用库,但是当页面加载时,有人可以帮我实现吗?

import React, { useRef, useEffect } from "react";
import * as tf from "@tensorflow/tfjs";
import Webcam from "react-webcam";
import { toast, ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
toast.configure();

function AppMain() { 

  const notify = () => toast("Fetching the Model Do not Close", {
    position: toast.POSITION.TOP_CENTER,
    autoClose: 60000});
 

  const webcamRef = useRef(null);
 

  };

  return (
    <div id="main" className="mainBlock">
      <div className="toasterbutton">
      
       
     <button onClick={notify}>Load Model</button>

     <ToastContainer />
    </div>
        <div id="caption"></div>
            <header style="margin-top:-2.5 rem;" className="container-fluid">
                <Webcam 
                ref={webcamRef}

                audio={false}
                muted={true} 
                style={{
                  position: "absolute",
                  marginLeft: "auto",
                  marginRight: "auto",
                  left: 0,
                  right: 0,
                  textAlign: "center",
                  zindex: 9,
                  width: 640,
                  height: 480,
            
                 }}
          
                 />

               
            </header>  
    </div>
  );
}

export default AppMain;

【问题讨论】:

    标签: reactjs react-toastify


    【解决方案1】:

    尝试将函数调用和the function itself 放在仅在挂载时运行的useEffect 中。

    function AppMain() { 
      const [show, hide] = useState(true);
      
      useEffect(() => {
        const notify = () => toast("Fetching the Model Do not Close", {
          position: toast.POSITION.TOP_CENTER,
          autoClose: 60000
        });
    
        notify();
      }, [])
    

    This answer 很好地解释了 useEffect 依赖数组。

    【讨论】:

    • 您好,谢谢它使用效果,但我只将通知放在使用效果中,将 const 通知放在它之外,谢谢l,如果可能的话,你能帮我如何出现另一个烤面包机通知一旦上一个烤面包机完成出现,我可以在使用效果中使用多个 const 吗?还是应该使用 usestate 或 if else 语句?
    • 顺便说一句,现在可以了,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多