Data Fetching
The usual recommendation is to use Vike's data()
hook for fetching initial data, and another dedicated tool for data mutations and subsequent data (such as pagination data).
Initial data
For fetching the initial data of a page you can use Vike's data()
hook, or you can use tools such as TanStack Query instead.
data()
hook
You can fetch the initial data of a page by using the Vike hook data()
together with the component hook useData()
.
pageContext
holds contextual information, see API >pageContext
.
The
@id
in the file path/pages/movie/@id/+data.js
denotes a route parameter which value is available atpageContext.routeParams.id
, see Guides > Routing.
useData()
is implemented by thevike-react
/vike-vue
/vike-solid
. If you don't usevike-react
/vike-vue
/vike-solid
then see API >useData()
> Withoutvike-{react,vue,solid}
.
The data()
hook can only be used for fetching the initial data of the page. For other use cases, such as data mutations and pagination data, use a data tool.
Tools
Some data-fetching tools have Vike extensions that enable your components to fetch their initial data:
vike-react-query
- TanStack Query integration forvike-react
vike-vue-query
- TanStack Query integration forvike-vue
vike-solid-query
- TanStack Query integration forvike-solid
- 🚧
vike-react-telefunc
- Telefunc integration forvike-react
Instead of using the
+data
hook, you can then use any of your components to fetch data, including your<Layout>
components.
Data mutation & subsequent data
For data mutation and subsequent data fetching (such as pagination data), use a data tool.
RPC
We generally recommend using RPC. It's simple, flexible, and performant.
For a list of RPC tools, see RPC.
API routes
A common alternative to RPC is to use API routes, see Guides > API Routes.
GraphQL
For large teams, it may make sense to use GraphQL instead of RPC.
With Vike, you can manually integrate GraphQL tools yourself, giving you complete control over integration:
In addition to manual integration, you will soon have the option to use Vike extensions for automatic integration.
Pre-rendering (SSG)
For pre-rendered pages / SSG apps, in order to fetch dynamic data from an external server, make sure to load and execute data()
only on the client-side, see API > data()
hook > Environment.
Global data
A common use case is to fetch global data that is needed by all pages. (For example i18n data.)
You can add your initial data to pageContext
at renderPage()
.
The pageContext
object is accessible from any Vike hook and any UI component, thus you can access your initialization data there as well.
If you use pre-rendering then see the workaround described at #962 - New hook onBoot()
.
State management
For managing complex UI state logic, you can use a store (Redux/Pinia/Zustand/...).
When using a store, all fetched data, including the initial data, is typically managed by the store. We recommend using an extension for automatic integration.
Extensions
Vike extensions for state management tools:
Custom integration
For complete control over integration, instead of using an extension, you can manually integrate a store yourself. See Integration > Stores (State Management).