When should we use Error Boundary components? Only for missing props and stuff like that?
For example, imagine this api fetching hook:
const useFetch = () => {
...
const [error, setError] = useState(null);
const method = async () => {
try {
await api.fetchData();
} catch(err) {
setError(err);
}
};
useEffect(() => {
method();
},[]);
return { ..., error };
}
Now, in a component, I just do:
const MyComponent = () => {
const { error } = useFetch();
if (error) return <FallbackUI />;
return <MainUI />;
}
Can I use an ErrorBoundary component to handle this situation (api call errors) instead of conditionally rendering?
EDIT
And what about if I only want to display a fallback UI when my fetching data method fails and there any data was previously retrieved?
Something like:
const { data, getMoreData, error } = useFetchPosts(); // data is stateful inside the hook
if (error && !data) return <FallbackUI />;
return <MainUI data={data} />;