1

我展示的代码是一个带有 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;
4

1 回答 1

1

尝试将函数调用和函数本身放在useEffect仅在挂载时运行的 a 中。

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();
  }, [])

这个答案很好地解释了 useEffect 依赖数组。

于 2022-01-03T07:25:48.923 回答