How to replace location.state? #284
Replies: 4 comments 14 replies
-
Any plan for this, and will it be typesafe? |
Beta Was this translation helpful? Give feedback.
-
I also have the same doubt. Does the group intend to implement? Today it is a determining factor for me. |
Beta Was this translation helpful? Give feedback.
-
For anyone else who ends up here trying to figure out how to use this like I did, this is what I was able to piece together... Yes, if you want it to be typed you have to declare the types as a global, but you have to pass a property instead of the whole state anyway, so you can declare different properties for different uses.. declare module '@tanstack/react-router' {
interface HistoryState {
user?: MyUserType;
project?: MyProjectType;
}
} To pass data to the route when navigating: const navigate = useNavigate();
navigate( { to : '/user', state : { user : someUserData } } ); Note that you can't just use To read the data from the page you navigated to: import { useRouterState } from '@tanstack/react-router';
export function UserRoute() {
const routerState = useRouterState();
const user = routerState.location.state.user;
} |
Beta Was this translation helpful? Give feedback.
-
Thanks for this @jasonk |
Beta Was this translation helpful? Give feedback.
-
Migrating from Reach Router v5 to React Location:
const history = useHistory();
history.replace({ pathname: '/', state: { name: 'Lily' }});
...
const location = useLocation();
// do sth with location.state...
How do I use React Location to replace the above code?
More detail, how do I use navigate with state?
Beta Was this translation helpful? Give feedback.
All reactions