Skip to content

[Bug]: navigate not working (not doing anything) when used inside useEffect #12140

@XHCui

Description

@XHCui

What version of React Router are you using?

v6.19.0

React version: v18.2.0

Steps to Reproduce

The user will access the page with url like '..../place/claim?...'

and the page have following code:

useEffect(() => {
    const getProfile = async () => {
       // api call getting stuff
       
       navigate('/place', { replace: true })
    }
    
    getProfile()
}, [...])

However, the navigate never works here

tried following as well:

const [shouldNavigate, setShouldNavigate] = useState(false);

useEffect(() => {
    const getProfile = async () => {
       // api call getting stuff
       
       setShouldNavigate(true);
    }
    
    getProfile()
}, [...])

if (shouldNavigate) {
  return (<Navigate to='/place' replace={true} />)
}

also doesn't redirect

Expected Behavior

url redirect to the /place

Actual Behavior

Nothing happens

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions