Seeing some weird things with a hook we use for dealing with apiCalls, where a sudden change in api response structure triggered infinite request loops in our app. I have slimmed it down to the bare requirements to reproduce the bug.
import React, {useEffect, useState, useMemo} from 'react'; const mockedApiCall = (resolvedValue, time) => new Promise((resolve, reject) => setTimeout(() => resolve(resolvedValue), time)); const useHook = (query) => { const [error, setError] = useState(null); const [data, setData] = useState([]); useEffect(() => { const asyncOperation = async () => { const response = await mockedApiCall([], 1000); // Testing something i know will fail: const mappedData = response.data.map(a => a); // if the above didn't fail we would update data setData(mappedData); }; // Clear old errors setError(null); // trigger operation asyncOperation().catch(e => { console.log('fail'); setError('Some error'); }); }, [query]); return [data, error]; } const HookIssue = props => { const [data, error] = useHook({foo: 'bar'}); // Creates infinite loop //const [data, error] = useHook('foo') // only fails a single time // Alternative solution that also doesn't create infinite loop // const query = useMemo(() => ({foo: 'bar'}), []); // const [data, error] = useHook(query); return null; }; export default HookIssue; Does anyone know what is happening here? The hook is supposed to only listen to changes in the query variable, but in some cases will just run into an infinite loop.
Edit: To add some more weirdness to this issue, if i remove either (or both) of the two setError calls inside the useEffect it would also prevent the infinite loop.