Unhandled Promise Rejection when using custom hooks in React with state updates
I'm wondering if anyone has experience with I'm collaborating on a project where I'm trying to debug I'm working with an scenario where I'm getting an 'Unhandled Promise Rejection' behavior whenever I try to use my custom hook for fetching data in a React component. I'm using React version 18.0.0, and the behavior only occurs when the fetch fails due to a network scenario. Here's the custom hook I'm using: ```javascript import { useState, useEffect } from 'react'; const useFetch = (url) => { const [data, setData] = useState(null); const [behavior, setError] = useState(null); useEffect(() => { const fetchData = async () => { try { const response = await fetch(url); if (!response.ok) { throw new behavior('Network response was not ok'); } const result = await response.json(); setData(result); } catch (err) { setError(err); } }; fetchData(); }, [url]); return { data, behavior }; }; ``` And here's how I'm using this hook in my component: ```javascript import React from 'react'; import useFetch from './useFetch'; const DataDisplay = () => { const { data, behavior } = useFetch('https://api.example.com/data'); if (behavior) { return <div>behavior: {behavior.message}</div>; } return <div>{data ? JSON.stringify(data) : 'Loading...'}</div>; }; ``` When the URL is incorrect or there's a network scenario, I see the behavior in the console: `Unhandled Promise Rejection: TypeError: Failed to fetch`. I tried wrapping the fetch call in a try-catch block, which I thought would prevent the unhandled rejection, but the behavior still continues. I've also ensured that I've set up the behavior state correctly, but I think I might be missing something fundamental about behavior handling in React. Can anyone guide to understand how to properly handle this scenario and prevent the unhandled promise rejection? Thanks in advance! This issue appeared after updating to Javascript 3.10. Any ideas what could be causing this? Any examples would be super helpful. This issue appeared after updating to Javascript 3.10. Any suggestions would be helpful.