exotic-emerald•3y ago
call some init function when the data received at edit page
How to call some init function when the data loaded on edit page, with antd
6 Replies
Hi @viveksingh0143. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
In Refine, when an Edit page loads, it fetches and fills the fields with data from the response. If you want to modify this data or call some function when the data is initially loaded, you can use the
useForm hook and useEffect hook from React.
Here's an example of how you can do it:
In this example, a state initialRender is used to track if it's the first render. When the component mounts, if it's the initial render and the form data is available, we modify the data using the setValue method from formMethods and then set initialRender to false.
If you want to modify the data when the Refresh button is clicked, you can add a dependency to the useEffect hook to listen for changes in the formState.data object. This will cause the useEffect hook to run whenever the data is refreshed.
This information is based on the extracts from the Refine Discord support thread (https://discord.com/channels/837692625737613362/1138081572709470228).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@940119979334967366> kapa.ai is still learning and improving, please let us know how it did by reacting below
eager-peach•3y ago
Hello @viveksingh0143
what do you mean by init function ?
All data hooks has
onSuccess callback. you can use this to do some actions after data fetched.
exotic-emeraldOP•3y ago
Hello @alicanerdurmaz
onSuccess called after form submission success,
I need to identify, when data loads for any resource at edit page
eager-peach•3y ago
for submission you need to use onMutationSuccess https://refine.dev/docs/packages/documentation/react-hook-form/useForm/#onmutationsuccess
https://refine.dev/docs/api-reference/antd/hooks/form/useForm/#onmutationsuccess sorry link was wrong
exotic-emeraldOP•3y ago
Thanks @alicanerdurmaz for document link