remix-run / react-router

Declarative routing for React

Home Page:https://reactrouter.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: Unable to Pass State Using useNavigate When Navigating Back to Previous Route

Sen-442b opened this issue · comments

What version of React Router are you using?

"^6.14.2"

Steps to Reproduce

  • Navigate to this sandbox.
  • Click on the "New Route" button to navigate to "/new-route".
  • On the "/new-route" page, attempt to navigate back to the home route by clicking the "Go Back" button.
  • The navigation back to the home route occurs, but the state passed during the navigation is not accessible in the home route.

Expected Behavior

Upon clicking the "Go Back" button, the application should navigate back to the home route, and the state passed during the navigation should be accessible in the home (redirected) route.

Actual Behavior

The navigation back to the home route occurs, but the state passed during the navigation is not accessible in the home route.

navigate(-1, { state: { message: 'message from new route' } }) //the message does not carry over to the redirected route.

This is expected behavior. The previous location is immutable. You cannot modify it by adding new state when navigating backwards. If you need to add state, you have a new location and need to navigate forwards. This is the same behavior as the native back() function.