小编hed*_*cox的帖子

React Hooks 详尽的异步无限循环

我有以下组件:

\n
import React, { useState, useEffect } from "react";\n\nconst App = () => {\n    const [data, setData] = useState<null | any[]>(null);\n    const [checked, setChecked] = useState(false);\n    const [loading, setLoading] = useState(false);\n\n    useEffect(() => {\n        setLoading(true);\n\n        (async () => {\n            if (data) {\n                // Could do something else here if data already exsisted\n                console.log("Data exists already");\n            }\n\n            const ret = await fetch("https://jsonplaceholder.typicode.com/users?delay=1000", { cache: "no-store" });\n            const json = await ret.json();\n            setData(json);\n            setLoading(false);\n        })();\n\n    }, [checked]);\n\n    return (\n        <>\n            <h1>useEffect …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs react-hooks eslint-plugin-react-hooks

8
推荐指数
1
解决办法
2917
查看次数