![](/img/trans.png)
[英]I cannot use the result of a custom hooks in a function or inside an effect hooks
[英]How can i use custom hooks inside my function?
我試圖在函數內使用鈎子,但是它不起作用,所以現在我嘗試發送一個回調函數來重置鈎子參數,但是它仍然無法正常工作。
如何在我的情況下使用此自定義掛鈎?
const del = useFetch(null);
const handleDelete = id => {
del.setParams({
hookUrl: "https://localhost:44354/api/car/" + id,
hookOptions: {}
});
console.log(del.response);
};
鈎子對GETS的工作正常,因為我沒有其他想要做的行為,但是對於刪除,我需要彈出一個模態,例如,我知道我可以直接使用提取,但只是出於好奇。
import React from "react";
const useFetch = (url, options) => {
debugger;
const [params, setParams] = React.useState({
hookUrl: url,
hookOptions: options
});
const [response, setResponse] = React.useState(null);
const [error, setError] = React.useState(null);
React.useEffect(() => {
const fetchData = async () => {
try {
const res = await fetch(params.hookUrl, params.hookOptions);
const json = await res.json();
setResponse(json);
} catch (error) {
setError(error);
}
};
fetchData();
}, []);
return { setParams, response, error };
};
export default useFetch;
const handleDelete = id => {
del.setParams({
hookUrl: "https://localhost:44354/api/car/" + id,
hookOptions: {}
});
console.log(del.response);
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.