[Solved] Uncaught Error: Maximum update depth exceeded (using axios post)
See original GitHub issueBug report
Description / Observed Behavior
Code keeps getting called repeatedly eventually ending in a Maximum update depth error.
Expected Behavior
Call should work as expected. I’m just trying to make a basic axios post request.
Repro Steps / Code Example
// App.tsx
import React from 'react';
import https from "https";
import axios from "axios";
import useSWR from "swr";
type myInput = {
myInputData: string;
};
export function App() {
const { data, error } = useSWR(
[
"/api/myEndpoint",
{
myInputData: "12345",
},
],
myFetcher
);
console.log("SWR response:", data);
return <></>
}
const myFetcher = async (url: string, input: myInput) => {
const agent = new https.Agent({
// custom agent config
});
const response = await axios({
method: "post",
url: url,
data: input,
httpsAgent: agent,
});
return response.data;
};
Additional Context
SWR version: 0.2.3
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
How to solve ' Maximum update depth exceeded' in my case?
Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate.
Read more >Fix the "Maximum Update Depth Exceeded" Error in React
One quick solution is to move the function inside the useEffect hook. ... We can see that now incrementViews is scoped inside the...
Read more >ReactJS - CodeProject
I'm trying to run my react native app Quote: This is the error I'm getting when i run"npm start" on cmd Error Expo...
Read more >The Complete Guide to React User Authentication with Auth0
The focus of this tutorial is to help developers learn how to secure a React application by implementing user authentication.
Read more >× Maximum update depth exceeded. This can happen when a ...
Error : Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
If you want to use objects with a single fetcher function instead of a custom one per useSWR call you can serialize it like this:
Something like that
But I’m not sure how performant that could be, you are going to serialize and deserialize a lot.
If you don’t enable Suspense on SWR, you may be able to also use
React.useMemo
to memoize the object and pass it directly, but this is also not 100% secure.Objects are not supported as part of a key, they are going to be regenerated on every render, instead try sending more values in the array or if possible move the definition of the object to outside the component (in your example is possible but most probably it will not when using it in an app)