How To use useNavigation 'create' function with additional query params or passing additional data
How to pass additional data when using useNavigation hook when navigating to create resource?
useNavigation hook's create function to navigate to a create resource, you can use the meta parameter. This allows you to include extra query parameters or additional data that can be accessed in the create form. Here's how you can do it:create function from useNavigation, you can pass a meta object as the third parameter:meta object you pass will be added to the URL as query parameters useNavigation API Reference (https://refine.dev/docs/routing/hooks/use-navigation/#api-reference).meta prop for the same purpose CreateButton Properties (https://refine.dev/docs/ui-integrations/chakra-ui/components/buttons/create-button/#properties).<Refine> component.useNavigation hook's create function in a Next.js app using the app router, you can use Next.js's built-in routing hooks. Specifically, you can use the hook from to access the query parameters.meta object you passed to the create function () will be added to the URL as query parameters. The hook allows you to access these parameters.meta object is typically transformed into URL query parameters, which is why we use to access them.useLocationuseLocationuseLocationuseLocationuseLocationuseLocationCreateButtonError: useLocation() may be used only in the context of a <Router> component.Error: useLocation() may be used only in the context of a <Router> component.@refinedev/nextjs-router@refinedev/nextjs-router@refinedev/nextjs-router<Router>usePathname()useSearchParams()next/navigationnext/navigationconst { create } = useNavigation();
// Usage
create("posts", "push", { authorId: "10" });const { create } = useNavigation();
// Usage
create("posts", "push", { authorId: "10" });useSearchParamsuseSearchParamsuseSearchParamsuseSearchParams{ authorId: "10" }import { useNavigation } from "@refinedev/core";
const { create } = useNavigation();
// Usage
create("posts", "push", { authorId: "10" });import { useLocation } from "react-router-dom";
const CreateForm = () => {
const location = useLocation();
console.log(location.state);
// This will log { authorId: '10' }
// Rest of your form logic
};import { Refine } from "@refinedev/core";
import routerProvider from "@refinedev/react-router-v6";
function App() {
return (
<Refine
routerProvider={routerProvider}
// ... other props
>
{/* Your app components */}
</Refine>
);
}import { Refine } from "@refinedev/core";
import routerProvider from "@refinedev/react-router-v6";
import { createBrowserRouter, RouterProvider, Outlet } from "react-router-dom";
const router = createBrowserRouter([
{
Component: RefineProvider,
children: [
// Your routes here
],
},
]);
const App = () => {
return <RouterProvider router={router} />;
};
const RefineProvider = () => {
return (
<Refine
routerProvider={routerProvider}
// ... other props
>
<Outlet />
</Refine>
);
};"use client";
import { Refine } from "@refinedev/core";
import routerProvider from "@refinedev/nextjs-router";
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
<Refine
routerProvider={routerProvider}
// ... other props
>
{children}
</Refine>
</body>
</html>
);
}"use client";
import { useSearchParams } from 'next/navigation';
const CreatePostPage = () => {
const searchParams = useSearchParams();
// Read the authorId from the query parameters
const authorId = searchParams.get('authorId');
console.log('Author ID:', authorId); // This will log: Author ID: 10
// Rest of your component logic
// ...
};
export default CreatePostPage;