Passing extra parameters in EditButton

I want to pass an extra query parameter when editing a record using the antd <EditButton> element. How can I pass an extra parameter to the details form?
11 Replies
kapa.ai
kapa.ai4mo ago
Hi @MesbearizingMeat. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- You can pass additional parameters to the edit method of useNavigation using the meta prop in the <EditButton> component. By default, existing parameters in the route are used by the edit method. You can pass additional parameters or override the existing ones using the meta prop. For example, if the edit action route is defined by the pattern: /posts/:authorId/edit/:id, the meta prop can be used as follows:
const MyComponent = () => {
return <EditButton meta={{ authorId: "10" }} />;
};
const MyComponent = () => {
return <EditButton meta={{ authorId: "10" }} />;
};
In this example, authorId is an additional parameter being passed to the edit method. For more information, refer to the documentation (https://refine.dev/docs/ui-integrations/ant-design/components/buttons/edit-button#meta). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@224018850419572736> kapa.ai is still learning and improving, please let us know how it did by reacting below
metropolitan-bronze
metropolitan-bronze4mo ago
@kapa.ai when I try that, it doesn't seem to actually add anything to the query string of the url. Is there a way to append the additional parameter to the query string so that it can then be fetched with useParsed()?
kapa.ai
kapa.ai4mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Currently, the meta prop in the <EditButton> component is used to pass additional parameters to the edit method of useNavigation, but it doesn't append these parameters to the query string of the URL. However, there's a workaround suggested in a Github issue (https://github.com/refinedev/refine/issues/5113) where you can swizzle the button and utilize the useGo hook. The useGo hook supports query parameters, which can be used to append additional parameters to the query string of the URL. Here's the link to the useGo hook in the documentation (https://refine.dev/docs/api-reference/core/hooks/navigation/useGo/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@224018850419572736> kapa.ai is still learning and improving, please let us know how it did by reacting below
metropolitan-bronze
metropolitan-bronze4mo ago
how do I access the parameters on the details form after passing them in the <EditButton> meta prop? @kapa.ai how do I access the parameters on the details form after passing them in the <EditButton> meta prop?
kapa.ai
kapa.ai4mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To access the parameters passed in the meta prop of the <EditButton> component, you can use the useParams hook from react-router-dom. This hook allows you to access the dynamic pieces of the URL. Here's an example of how you can use it:
import { useParams } from 'react-router-dom';

const MyComponent = () => {
const { authorId } = useParams();

// Now you can use authorId in your component
};
import { useParams } from 'react-router-dom';

const MyComponent = () => {
const { authorId } = useParams();

// Now you can use authorId in your component
};
In this example, authorId is the additional parameter that was passed in the meta prop of the <EditButton> component. For more information, refer to the documentation (https://refine.dev/docs/routing/hooks/use-navigation/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@224018850419572736> kapa.ai is still learning and improving, please let us know how it did by reacting below
metropolitan-bronze
metropolitan-bronze4mo ago
@kapa.ai okay, so how would I swizzle the <EditButton> and then use the useGo to pass my parameter as a query parameter?
kapa.ai
kapa.ai4mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To swizzle the <EditButton> and use the useGo hook to pass your parameter as a query parameter, you can follow these steps: 1. First, import the necessary hooks and components:
import { useGo } from "@pankod/refine";
import { Button } from "antd";
import { EditOutlined } from "@ant-design/icons";
import { useGo } from "@pankod/refine";
import { Button } from "antd";
import { EditOutlined } from "@ant-design/icons";
2. Then, create your custom EditButton component:
const MyEditButton = ({ record, ...props }) => {
const go = useGo();

const handleClick = () => {
go({
to: {
resource: "posts",
action: "edit",
id: record.id,
},
query: {
myParam: "myValue", // replace with your parameter
},
type: "push",
});
};

return (
<Button
onClick={handleClick}
icon={<EditOutlined />}
{...props}
/>
);
};
const MyEditButton = ({ record, ...props }) => {
const go = useGo();

const handleClick = () => {
go({
to: {
resource: "posts",
action: "edit",
id: record.id,
},
query: {
myParam: "myValue", // replace with your parameter
},
type: "push",
});
};

return (
<Button
onClick={handleClick}
icon={<EditOutlined />}
{...props}
/>
);
};
In this example, myParam is the additional query parameter that you want to pass. Replace "myValue" with the value of your parameter. 3. Finally, use your custom MyEditButton component in place of the <EditButton> component. For more information, refer to the useGo hook in the documentation (https://refine.dev/docs/api-reference/core/hooks/navigation/useGo/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@224018850419572736> kapa.ai is still learning and improving, please let us know how it did by reacting below