reduxjs / redux-toolkit

The official, opinionated, batteries-included toolset for efficient Redux development
https://redux-toolkit.js.org
MIT License
10.55k stars 1.13k forks source link

RTK Query: placeholder data #1445

Open stevenliebregt opened 2 years ago

stevenliebregt commented 2 years ago

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.

phryneas commented 2 years ago
const { data = myDefaultValue } = useMyCoolQuery();

would do the trick :)

stevenliebregt commented 2 years ago

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

y471n commented 2 years ago

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

markerikson commented 2 years ago

@phryneas , anything we want to consider doing here?

nhayfield commented 1 year ago

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.