Pass data between routes #1342
-
How can I pass data between routes? I tried this but it is not working. (Also tried passing formData as a prop to ReviewForm.) MainForm.tsx const navigate = useNavigate({ from: '/' })
const handleFormSubmit = (formData) => {
navigate({ to: '/review', params: { formData }}, )
} ReviewForm.tsx export const Route = createFileRoute('/review')({
component: ReviewForm
})
function ReviewForm() {
const {formData} = Route.useParams()
console.log(formData);
return (
<>Review Form</>
)
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
The concept of params relates to path parameters. For example, in this path navigate({ to: '/posts/$postId', params: { postId: '123' } }); What you are looking for might be similar to the browser native window.history.pushState.
If you want to use it, you'll need to register the types for this // Register things for typesafety
declare module '@tanstack/react-router' {
interface Register {
router: typeof router;
}
interface HistoryState {
post?: { id: string; name: string };
}
} This declaration will allow you to optionally pass in key of <Link
to='/posts/$postId'
params={{ postId }}
state={{ post: { id: '123'; name: 'foo' } }}
>
{"Go to post"}
</Link>
// or
const navigate = useNavigate();
navigate({
to: '/posts/$postId',
params: { postId }
state: {{ post: { id: '123'; name: 'foo' } }}
}) Finally, you'll be able to access this state property using the useRouterState hook. const state = useRouterState({ select: s => s.location.state });
// ^ { post?: { id: string; name: string } }
|
Beta Was this translation helpful? Give feedback.
The concept of params relates to path parameters. For example, in this path
/posts/$postId
, the param is the$postId
. As such, by default, all the path parameters arestring
.So this navigation would look like:
What you are looking for might be similar to the browser native window.history.pushState.
Tanstack Router does allow you to pass data between routes during navigation using the
state
field.If you want to use it, you'll need to register the types for this
HistoryState
u…