xenial-black
xenial-black4w ago

passing param when navigating to another page useNavigation

passing param when navigating to another page useNavigation e.g. how to populate sid here? export const ReservationCreate = ({ sid }) => {
console.log(sid = ${sid}); }
5 Replies
kapa.ai
kapa.ai4w ago
Hi @flankieflew. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To pass parameters when navigating to another page using the useNavigation hook in refine, you can use the meta property. Here's how you can do it: 1. When navigating, use the meta property to pass additional parameters:
import { useNavigation } from "@refinedev/core";

const { create } = useNavigation();

// Navigate to the create page with additional parameters
create("reservations", { meta: { sid: "123" } });
import { useNavigation } from "@refinedev/core";

const { create } = useNavigation();

// Navigate to the create page with additional parameters
create("reservations", { meta: { sid: "123" } });
2. In the target component (ReservationCreate), you can access the sid parameter using the useParams hook from react-router-dom:
import { useParams } from "react-router-dom";

export const ReservationCreate = () => {
const params = useParams();
const sid = params.sid;

console.log(`sid = ${sid}`);

// Rest of your component logic
}
import { useParams } from "react-router-dom";

export const ReservationCreate = () => {
const params = useParams();
const sid = params.sid;

console.log(`sid = ${sid}`);

// Rest of your component logic
}
The meta property allows you to pass additional parameters when navigating as described in the refine documentation (https://refine.dev/docs/routing/hooks/use-navigation/#return-values). This approach works for all navigation methods provided by the useNavigation hook, including list, create, edit, and show. Remember that for this to work, you need to define your routes accordingly. For example, your route definition might look like this:
<Route path="/reservations/create/:sid" element={<ReservationCreate />} />
<Route path="/reservations/create/:sid" element={<ReservationCreate />} />
This way, the sid parameter will be part of the URL and can be accessed using useParams. React Router's useParams documentation (https://refine.dev/blog/react-router-useparams/#how-to-use-the-useparams-hook) provides more details on how to use the useParams hook to retrieve URL parameters in your components. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@343691288442306561> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
xenial-black
xenial-blackOP4w ago
how to use MetaQuery @kapa.ai how to use MetaQuery
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- To use MetaQuery in refine, you can leverage the meta property in various hooks and components. The meta property allows you to pass additional parameters when navigating or performing actions. Here's how you can use it: 1. In navigation hooks and components: You can use the meta property with navigation hooks like useNavigation or navigation components like ShowButton, EditButton, etc. For example:
<ShowButton meta={{ authorId: "10" }} />

<ShowButton meta={{ authorId: "10" }} />

This is useful when your route is defined with additional parameters, like /posts/:authorId/show/:id Mantine Show Button (https://refine.dev/docs/ui-integrations/mantine/components/buttons/show-button/#meta). 2. In data hooks: You can also use the meta property in data hooks like useTable, useForm, etc. For example:
useTable({
resource: 'posts',
meta: { authorId: "10" }
});

useTable({
resource: 'posts',
meta: { authorId: "10" }
});

This can be particularly useful when working with nested resources or when you need to pass additional context to your API calls Github Issue Comment (https://github.com/refinedev/refine/issues/1724#issuecomment-1114183893). 3. In resource definitions: You can define a default meta for a resource in the <Refine> component:
<Refine
resources={[
{
name: "posts",
meta: { authorId: "10" }
}
]}
/>

<Refine
resources={[
{
name: "posts",
meta: { authorId: "10" }
}
]}
/>

This default meta will be used when no specific meta is provided in the hooks or components Github Discussion (https://github.com/refinedev/refine/discussions/6175). 4. In custom routes: When defining custom routes, you can use the :param syntax in your action definitions. Refine will look for the provided meta value, the meta value from the resource definition, and the params from the router to handle navigation and route matching React Router Integration (https://refine.dev/docs/routing/integrations/react-router/#usage-with-a-root-route). Remember, the meta property is flexible and can be used to pass any additional parameters you need. It's particularly useful for handling nested resources, passing context to API calls, or managing complex routing scenarios. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@343691288442306561> Kapa.ai is still learning and improving, please let me know how I did by reacting below.