useData()
Environment: server, client.
Implemented by: UI framework Vike extension vike-react
/vike-vue
/vike-solid
(or yourself).
The useData()
component hook allows you to access the value returned by the data()
hook.
See Guides > Data Fetching for an introduction about
useData()
and fetching data in general.
You can also access the data over
pageContext.data
. In factconst { data } = usePageContext()
returns the same value asuseData()
.Using
pageContext.data
instead ofuseData()
makes sense if you want to access the data in Vike hooks sinceuseData()
only works inside UI components (not inside Vike hooks).
For example:
See Guides > Data Fetching for more information about data fetching.
TypeScript
Using type inference as shown above is usually what you want, but if you want to enforce a type instead:
Without vike-{react,vue,solid}
In case you don't use a UI framework Vike extension vike-react
/vike-vue
/vike-solid
, you can implement useData()
yourself.
In general, for improved DX, we recommend using a
useData()
implementation. But you don't have to as shown at API >data()
hook > Withoutvike-{react,vue,solid}
.
Examples:
- /boilerplates/boilerplate-react/renderer/useData.js (React + JavaScript)
- /boilerplates/boilerplate-react-ts/renderer/useData.ts (React + TypeScript)
- /boilerplates/boilerplate-vue/renderer/useData.js (Vue + JavaScript)
- /boilerplates/boilerplate-vue-ts/renderer/useData.ts (Vue + TypeScript)
vike-react
> /hooks/useData.tsxvike-vue
> /hooks/useData.tsvike-solid
> /hooks/useData.tsx