redux-toolkit icon indicating copy to clipboard operation
redux-toolkit copied to clipboard

RTK Query: placeholder data

Open stevenliebregt opened this issue 2 years ago • 5 comments

Is there any way to provide placeholder data when executing a query?

For example if I have the following:

const { data } = useMyCoolQuery();

I'd like data to contain a default value instead of just being undefined while it is still loading.

stevenliebregt avatar Aug 26 '21 14:08 stevenliebregt

const { data = myDefaultValue } = useMyCoolQuery();

would do the trick :)

phryneas avatar Aug 26 '21 14:08 phryneas

Yeah, but that means that anywhere I'd like to use my query I'd have to add a default value. I'd like for that to be possible on a slightly higher level, preferably the same place as I'm defining my request

stevenliebregt avatar Aug 26 '21 14:08 stevenliebregt

Something similar to React Query would be great: https://react-query.tanstack.com/guides/placeholder-query-data

y471n avatar Oct 27 '21 14:10 y471n

@phryneas , anything we want to consider doing here?

markerikson avatar Jun 29 '22 02:06 markerikson

what about a way to extract the default/example values from the openapi spec and instantiate new objects from the generated types that include the defaults. this could be used as described above in the query. but it would also be useful for new records that have no data yet.

nhayfield avatar Jul 05 '23 15:07 nhayfield