Reactjs 如何在函数中使用自定义挂钩?
我尝试在函数中使用钩子,但它不起作用,所以现在我尝试发送一个回调函数来重置钩子参数,但它仍然不起作用 我怎样才能使这个定制钩子在我的情况下工作Reactjs 如何在函数中使用自定义挂钩?,reactjs,react-hooks,Reactjs,React Hooks,我尝试在函数中使用钩子,但它不起作用,所以现在我尝试发送一个回调函数来重置钩子参数,但它仍然不起作用 我怎样才能使这个定制钩子在我的情况下工作 const del = useFetch(null); const handleDelete = id => { del.setParams({ hookUrl: "https://localhost:44354/api/car/" + id, hookOptions: {} }); co
const del = useFetch(null);
const handleDelete = id => {
del.setParams({
hookUrl: "https://localhost:44354/api/car/" + id,
hookOptions: {}
});
console.log(del.response);
};
钩子对于GETS工作得很好,因为我没有任何其他我想做的行为,但是对于delete,我需要弹出一个模态,例如,我知道我可以直接使用fetch,但只是好奇而已
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;
useFetch()
必须提供两个参数,您刚才给它的是null
?setParams还接受一个对象,但您提供了两个值:string和empty object。检查这一点,我确信你会很好,就像我应该发送json的响应一样,我更正了它,但它仍然不起作用。我猜我只是无法在函数中使用fetch,无论我怎么做:(@ClaeusdevI,我想你可能想为选项
传递另一个空值,然后看
const handleDelete = id => {
del.setParams({
hookUrl: "https://localhost:44354/api/car/" + id,
hookOptions: {}
});
console.log(del.response);
};