Documentation
Getting Started
Functions
Guides
Concepts
Client Query
How to fetch on client with fresh dataTo fetch fresh data in Client Components, use the clientQuery
function.
When fetching fresh data in Client Components, the following sequence occurs:
Step | Environment | Description |
---|---|---|
Pre | Browser | Sends a request to the server for the website at https://example.com . |
Pre | Server | Sends the initial HTML response to the browser. |
1 | Browser | Receives the response, hydrates the page and sends a request to the API for the data. |
2 | Browser | Receives the fresh data and displays the data. |
The following example fetches data on the client side using a GET request.
To ensure the data is always fresh, either set queryCache
to false or omit it entirely.
"use client";
import { clientQuery } from "@bigbang-sdk/next-query";
function ClientComponent() {
const { data, error, isLoading } = clientQuery({
queryUrl: "https://api.example.com/data",
});
console.log(data, error, isLoading);
}
The following example fetches data on the client side using a POST request.
To ensure the data is always fresh, either set queryCache
to false or omit it entirely.
"use client";
import { clientQuery } from "@bigbang-sdk/next-query";
function ClientComponent() {
const { data, error, isLoading } = clientQuery({
queryUrl: "https://api.example.com/data",
queryOptions: {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ foo: "bar" }),
},
});
console.log(data, error, isLoading);
}
Made with
by Bigbang