question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Solved] Uncaught Error: Maximum update depth exceeded (using axios post)

See original GitHub issue

Bug 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:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
sergiodxacommented, Jul 21, 2020

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:

function fetcher(url: string, serializedParams: string) {
  const params = JSON.parse(serializedParams);
  // code here
}

useSWR(["/api", JSON.stringify({ token: userToken })], { fetcher })

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.

2reactions
sergiodxacommented, Jul 21, 2020

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)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found