Here is my code:
const useMyFetch = (url, options) =>
{
const [response, setResponse] = React.useState(null);
React.useEffect(() =>
{
console.log("going to fetch ", url);
fetch(url, options).then(async function(response)
{
var json = await response.json();
setResponse(json.message);
});
}, [ url ]);
return response;
};
function Example()
{
const res = useMyFetch("https://dog.ceo/api/breeds/image/random", { method: 'GET' });
if (!res)
{
return <div>loading...</div>
}
return <img src={res} alt="an image" />;
}
It looks that everything is fine... except when I replace the second argument of useEffect from [ url ] to [ url, options ]. When 'options' is there, we're entering in the well known infinite loop... however it's logical to have it in this array. What's wrong here? Thanks