因此,我们可以不直接在 useEffect 里使用用 async 函数,需要把函数提取出来,像下面这样: 代码语言:javascript 代码运行次数:0 importReact,{useState,useEffect}from'react';importaxiosfrom'axios';functionApp(){const[data,setData]=useState({hits:[]});constfetchData=async()=>{constresult=awaitaxios('http:/...
importReact,{useState,useEffect}from'react';importaxiosfrom'axios';functionApp(){const[data,setData]=useState({hits:[]});useEffect(()=>{constfetchData=async()=>{constresult=awaitaxios('https://hn.algolia.com/api/v1/search?query=redux',);setData(result.data);};fetchData();},[]);return...
function useFetchDataInterval(fetchData) { const [list, setList] = useState([]); useEffect(() => { let id; const controller = new AbortController(); async function getList() { try { const data = await fetchDataWithAbort({ fetchData, signal: controller.signal }); setList(list => list...
useEffect(() => { (async function fn() { const response = await selectReceiveMaterialRecord({ billDetailId: billDetailId, }); form.setFieldsValue({ materielId: response.data?.materialName }); setMaterielId(response.data?.materialId); setPickList(response.data?.recordList); response.data?....
async function fetchData(params) { const res = await axios.get('/url', { params }); appDispatch({ type: "LOAD_ELEMENTS", elements: res.data }); } return ( <ChildComponent updateData={fetchElements} /> . . . ) }; 我想
}elseif(typeofdestroy.then==='function') { addendum ='\n\nIt looks like you wrote useEffect(async () => ...) or returned a Promise. '+'Instead, write the async function inside your effect '+'and call it immediately:\n\n'+'useEffect(() => {\n'+' async function fetchData() ...
Approach 1: Create an asynchronous function (async...await method), and then execute the function. useEffect(() => { const asyncFun = async () => { setPass(await mockCheck()); }; asyncFun(); }, []); Approach 2: You can also use IIFE, as follows: ...
} else if (typeof destroy.then === 'function') { addendum = '\n\nIt looks like you wrote useEffect(async () => ...) or returned a Promise. ' + 'Instead, write the async function inside your effect ' + 'and call it immediately:\n\n' + 'useEffect(() => {\n' + ' async...
function App({signOut, user}) { const [weekly, setWeekly] = useState([]); useEffect(() => { WebFont.load({ google: { families: ['DM Sans', 'Inter'] } }); async function fetchWeekly(queryTemplate, queryVars) { try { const weeklyData = await API.graphql(graphqlOperation(queryTem...
Put the async function inside: async function fetchData() { useEffect(async () => {接下来是这样的 浏览2提问于2022-05-06得票数 1 1回答 异步redux分派会导致内存泄漏。 、、 我如何解决这个问题? } }, []); fetchData(); }, [fetchData]); 浏览5提问于2020-07-29得票数 0 2回答 在useEffe...