history.push() generates infinite loop
See original GitHub issueHi! 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:
- Created 4 years ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top 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 >
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
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”) },
React Router is better handled on new v2 version released under scoped
@reactour
package name, please trynpm i @reactour/tour
. Here is a sandbox example. Every feedback super welcome, thanks!