Skip to content

next/router - push function changes between renders when called #18127

@dan-cooke

Description

@dan-cooke

Bug report

Depending on the push function of useRouter causes an infinite loop when the push function is called from inside a useEffect

To Reproduce

  1. Set up a catch-all optional dynamic route [[...slug]].js
  2. Render the following component from this page
    Example component
const BugExample = () => {
    useEffect(() => {
        push('/[[...slug]]', '/123/test', { shallow: true })
        console.log('called');
     },[push])

    return null
}

You will see an infinite loop of called in the console

Expected behavior

Push should be a non-changing reference, so that the react diff algoirithm knows not to call side effects again uneccesarily

System information

  • OS: macOs
  • Browser: Chrome
  • Version of Next.js: 9.5
  • Version of Node.js: 13.2.0

NEXT-1375

Metadata

Metadata

Assignees

No one assigned

    Labels

    linear: nextConfirmed issue that is tracked by the Next.js team.locked

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions