How to handle nested data object returned from useQuery #2929
-
I am new to the library and was wondering what is considered best practice for consuming the properties from a query that returned a nested data object. for example if my query returns: I would like to destructure items and count from the query data in the component that calls the query, but I don't know if using placeholderData or initialData is considered the proper use case for this. I don't want to have to null check data and data[key] everywhere the properties are used so I was wondering what experienced users would advise. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
placeholderData and initialData make sure of this at runtime, but if you use TypeScript, you'll still be forced to null check. Read about the differences between the two settings here: https://tkdodo.eu/blog/placeholder-and-initial-data-in-react-query destructuring with default values makes sure that you don't have to null check in your component, but you need to come up with defaults. |
Beta Was this translation helpful? Give feedback.
placeholderData and initialData make sure of this at runtime, but if you use TypeScript, you'll still be forced to null check. Read about the differences between the two settings here: https://tkdodo.eu/blog/placeholder-and-initial-data-in-react-query
destructuring with default values makes sure that you don't have to null check in your component, but you need to come up with defaults.