简体   繁体   English

next.js 中的 UseEffect 钩子导致了 2 个获取请求,而我预期是 1 个,为什么?

[英]UseEffect hook in next.js is causing 2 fetch requests when I expect 1, why?

The following code is causing 2 requests to be made to my API with the GET endpoint.以下代码导致使用 GET 端点向我的 API 发出 2 个请求。 I don't understand useEffect() enough to know where to start to debug, but I don't want to cause unneccessary strain on the services that these API endpoints will call server side.我对 useEffect() 的了解还不足以知道从哪里开始调试,但我不想对这些 API 端点将调用服务器端的服务造成不必要的压力。 This is because they will be calling windows servers to action delete requests in active directory etc...这是因为他们将调用 windows 服务器来处理活动目录中的删除请求等......

function Ad() {
    const [data, setData] = useState(null)
    const [isLoading, setLoading] = useState(false)
    useEffect(() => {
        setLoading(true)
        fetch('../api/decom/ad')
            .then(response => response.json())
            .then(data => {
                setData(data)
                setLoading(false)

                if (data.exists == true) {
                    Remove()
                    }
            })
    }, [])

    const Remove = () => {

        fetch("../api/decom/ad", { method: "DELETE" })
            .then(response => response.json())
            .then(data => {
                var inner = ""
                if (data.removed == true) {
                    inner = `<p>Removed: ${greenCircle}</p>`
                } else {
                    inner = `<p>Removed: ${redCircle}</p>`
                }

                document.getElementById('adremoved').innerHTML = inner

            })
    }

    if (data != null  && data.exists == true) return (
        <a className={styles.card}>
            <h2>AD</h2>
            <p>Exists: { greenCircle } </p>
            <div id ="adremoved" className={styles.parent}>
                <p className={styles.child}>removed:</p>
                <div><div className={styles.loader}><div></div><div></div><div></div></div></div>
            </div>
        </a>
    )

    if (data != null && data.exists == false) return (
        <a className={styles.card}>
            <h2>AD</h2>
            <p>Exists: {redCircle} </p>
            <p>Removed:{redCircle}</p>
        </a>
        )

    if (isLoading) return (
        <a className={styles.card}>
            <h2>AD</h2>
            <div className={styles.parent}>
                <p id="adstatus" name="adstatus" className={styles.child}>Exists:</p>
                <div id="adloader"><div className={styles.loader}><div></div><div></div><div></div></div></div>
            </div>
            <p>Removed:{redCircle}</p>
        </a>
        )
}
const [isLoading, setLoading] = useState(true)
    useEffect(() => {
       if(isLoading){
        fetch('../api/decom/ad')
            .then(response => response.json())
            .then(data => {              
                if (data.exists == true) {
                    Remove()
                    }else{
                setData(data)                
                }
               setLoading(false)
            })
       }
    }, [isLoading])

If you are using react 18, useEffect hook will run twice, that is the expected behaviour如果您使用的是 React 18,useEffect 钩子将运行两次,这是预期的行为

React 18 introduces a new development-only check to Strict Mode. This new check will automatically unmount and remount every component, whenever a component mounts for the first time, restoring the previous state on the second mount.

https://reactjs.org/docs/strict-mode.html#ensuring-reusable-state https://reactjs.org/docs/strict-mode.html#ensuring-reusable-state

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Next.js 不在生产应用程序中调用 useEffect 钩子 - React Next.js not calling useEffect hook in production app Next.js 数据获取 useSWR 还是使用 useEffect 的常规 fetch/axios? - Next.js data fetching useSWR or regular fetch/axios with useEffect? Next.js - 访问同一路由时,useEffect 不会触发 - Next.js - When accessing the same route, useEffect is not firing Next.js 500 Internal Server Error 在 404 页面内调用 useEffect Hook 后立即出现错误 - Next.js 500 Internal Server Error right after calling useEffect Hook inside 404 Page 在生产中构建 Next.js static 网站时获取错误 - Fetch error when building Next.js static website in production 错误:无效挂钩调用。 | 使用 getStaticProps (Next.js) 中的上下文获取数据的问题 - Error: Invalid hook call. | Problem to fetch data using context inside of getStaticProps (Next.js) 无效的钩子调用,next.js/react - Invalid hook call, next.js/react 即使在 useEffect 中也没有定义带有 next.js window 的 P5js - P5js with next.js window is not defined even in useEffect 为什么我在 getInitialProps 中使用 params 在 Next.js 中构建时它们是未定义的 - Why I use params in getInitialProps they are undefined when build in Next.js 当我在 Next.JS 中更改页面时,整个页面都会更新。 为什么? - When I change page in Next.JS whole page gets updated. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM