Overwriting defaultValues from React Hook Form with data from useQuery
Hello, I am having trouble mixing useQuery from React Query together with React Hook Form
The
eventDetailsToEdit is supposed to reset the empty values, but I keep getting an error value is undefined in the browser, however, on older tabs I have open on the page, the spread operator does what it's supposed to, I am assuming it's due to cache, however when refreshing the same error pops up11 Replies
genetic-orangeโข3y ago
Hi ๐
Please could you provide a runnable minimal reproducible example?
I'm happy to take a look at this for you but I'm not an avid React Hook Form user, so it'd be easier if I could look at the bigger picture and test things ๐
inland-turquoiseOPโข3y ago
I guess I could paste the entire file, it won't work due to dependencies and missing server data, would that be useful?
genetic-orangeโข3y ago
A runnable example like a Code Sandbox would be ideal. I can't run your file on its own so it wouldn't be much help unfortunately
Equally, feel free to wait and see if someone who's used both of these libraries in conjunction with each other sees this and notices anything immediately obvious
inland-turquoiseOPโข3y ago
Ah, there's too much in that case, unfortunately
genetic-orangeโข3y ago
A minimal reproducible example is exactly what the name suggests. In this case, you'd need a query with a query function that resolves to some data, and a form with a field or two via React Hook Form. It's up to you ๐
inland-turquoiseOPโข3y ago
I will try to do that once I get home, it was always puzzling to me how server data is reproduced to a sandbox, though I assume it's changed to use dummy data
genetic-orangeโข3y ago
You can just return a Promise that resolves to whatever you want to mock
You don't need to make network requests or anything like that
inland-turquoiseOPโข3y ago
Sorry for the late response, I am facing the same issue in the sandbox ๐
https://codesandbox.io/s/jovial-platform-klgtkn?file=/src/App.js:1003-1005
The idea was to spread the
data object, which would overwrite the empty values, however, that doesn't seem to work, but I don't know whygenetic-orangeโข3y ago
useForm
Performant, flexible and extensible forms with easy-to-use validation.
genetic-orangeโข3y ago
but also read about
resetOptionsgenetic-orangeโข3y ago
it was introduced in 7.41.0 to better introp with async state managers: https://github.com/react-hook-form/react-hook-form/releases/tag/v7.41.0
GitHub
Release ๐ Version 7.41.0 ยท react-hook-form/react-hook-form
๐ NEW values props
The following syntax will react to values prop update/changes.
values will be reactive to update/change and reset accordingly
provide a reset option to keep dirty/touched values...