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.

history.push() generates infinite loop

See original GitHub issue

Hi! I am trying to go to another location, using history.push() but it generates this error:

Invariant Violation: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

My code looks like this:

export const App = (props) => {
  //reactour
  const [isTourOpen, setOpenTour] = useState(false);

  const openTour = () => {
    setOpenTour(true)
  }
  const closeTour = () => {
    setOpenTour(false)
  }
  return (
    <div >
      <button onClick={openTour}>Open tour</button>
      <Tour isTourOpen={isTourOpen} closeTour={closeTour} />
    </div>
  );
}

const Tour = withRouter(
  ({ isTourOpen, closeTour, location: { pathname }, history }) => {
    const steps = [
      {
        selector: '.first-step',
        content: 'Step 1',
      },
      {
        selector: '[data-tut="tour-burger"]',
        content: 'Step 2!'
      },
      {
        selector: '[data-tut="tour-userMenu"]',
        content: 'Step 3'
      },
      {
        selector: '[data-tut="tour-about"]',
        content: 'Go to another location!',
        action: () => history.push('/about') //<-- ** This line creates an eternal loop **
      },
      {
        selector: '.last-step',
        content: 'This is my last Step',
      }
    ]
    return (
      <Reactour
      steps={steps}
      isOpen={isTourOpen}
      onRequestClose={closeTour}
      update={pathname}
      />
    );
  }
)

By changing the below line of code I can prevent the eternal loop - but it still seems to trigger three times ( ? )

//Old code
action: () => history.push('/about') //<-- ** This line creates an eternal loop **
//Updated code
action: (node) => {console.log('node', node); (!node) ? history.push('/about') : null}

Console log:

node undefined node < h1 data-tut=“tour-about” > node < h1 data-tut=“tour-about” >

Am I missing something basic here? Any help much appreciated! 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
nurrgurcommented, Dec 23, 2021

Merhaba, ben sorunu bu şekilde çözdüm.

const changeHistory=(link)=>{ if(op == 0) { history.push(link); setOp(1) setTimeout(() => { setOp(0) }, 200); } } window.changeHistory = changeHistory;


action: () => { window.changeHistory(“/settings/orders”) },

0reactions
elrumordelaluzcommented, Sep 8, 2021

React Router is better handled on new v2 version released under scoped @reactour package name, please try npm i @reactour/tour. Here is a sandbox example. Every feedback super welcome, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Router history.push in infinite loop - Stack Overflow
This problem occurs when you changes value of variable in useEffect and that variable also added in useEffect dependency array, ...
Read more >
How can I avoid infinite loops in my React Router private routes?
How to nest routes in React Router v4? How can I pass data using <Redirect> in react router v4? How can I use...
Read more >
useEffect on router query causes infinite loop : r/nextjs - Reddit
I'm building a search/filter functionality that just appends the new filter as a param to the URL, and then SWR can read from...
Read more >
useHooks - Easy to understand React Hook recipes
We bring you easy to understand React Hook code recipes so you can learn how React hooks work and feel more comfortable writing...
Read more >
5 useEffect Infinite Loop Patterns | by Naveen DA
When you run this code, it will throw Maximum update depth exceeded which means the code having an infinite loop. What's happening? Since ......
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