Gadget provides the @gadgetinc/react library of React hooks for reading and writing data from your Gadget models. The useFindOne, useFindMany, useFindBy, and useGet hooks read data from your backend models and render UI with React.
Each of these hooks returns an object with the requested data, the fetching state, and an error if one was encountered, as well as a refetch function for refreshing the data if need be.
For example, if you have a model named post, we can fetch post records in a variety of ways:
For more documentation on the @gadgetinc/react hooks library, see the React reference.
Writing data to models
The @gadgetinc/react React hooks library includes the useActionForm hook and the useAction hook for writing data to models by running Actions. useActionForm is suitable for building forms that give users inputs to control the input data, and useAction is a lower level hook for calling actions directly.
Building forms
With the useActionForm hook, you can manage form state and call actions easily. For example, we can build a form for a post model with useActionForm:
See the Forms guide for comprehensive docs on building forms with useActionForm.
Calling Actions directly
If you aren't building a form, or if you need lower-level control, the useAction hook can be used to call actions directly. useAction returns two values: a result object with the data from running, the fetching state, and the error if one was encountered, as well as an act function to run the backend action.
For example, with a post model on the backend, we can create a new post record by calling the act function returned by the useAction hook:
useGlobalAction returns two values: a result object with the data from running, the fetching state, and the error if one was encountered, as well as an act function to run the backend global action.
For example, if you have a Global Action named syncData, we can run this action by calling the act function returned by the useGlobalAction hook:
Backend HTTP Routes are available for calling from your frontend codebase. Calling these routes can be done with any HTTP client, but within the frontend, Gadget recommends using the useFetch hook. useFetch provides a pleasant React wrapper around the built-in browser fetch function, and includes automatic authentication support.
In a frontend React component, useFetch will make a request to a backend HTTP route. For example, if we have a routes/GET-hello.js file that sends a JSON reply in our backend like this:
routes/GET-example.js
JavaScript
module.exports=async(request, reply)=>{
reply.send({message:"Hello from the backend!"});
};
We can call this route in a frontend React component:
6console.log(data);// will start out null, then when the data arrives, { message: "Hello from the backend!" }
7};
Calling fetches imperatively
If you aren't using React, or would like to await a request like you might with the built-in browser fetch function, you can use the api.fetch function:
For example, we can call the /hello route from above like this:
JavaScript
const result =await api.fetch("/hello").json();
console.log(result);// { message: "Hello from the backend!" }
api.fetch is appropriate for use in an imperative context, like a server-side script, or other places where you don't need to give the user feedback about what's happening. useFetch is appropriate when you need to show the user feedback as the fetching or error state changes.
Maintaining session state when calling HTTP routes
Different apps use different mechanisms to authenticate requests from the client to your Gadget backend. When making raw HTTP calls to your backend, you need to ensure that the correct authentication headers are passed to your backend. The api client object provided by Gadget sends these headers automatically for GraphQL requests and requests made by the React hooks.
The useFetch hook and api.fetch function implements this same automatic authentication header setting but for any HTTP request to the backend. useFetch and api.fetch wrap the browser built-in fetch, but add the headers required to implement whichever authentication mode is active for your app.
The different authentication modes are documented in your API reference.
useFetch(path: string, options: RequestInit = {})
useFetch is a low-level hook for making an HTTP request to your Gadget backend's HTTP routes. useFetch preserves client-side authentication information by using api.fetch under the hood, which means fetches will use the same request identity as other GraphQL API calls using the other hooks.
useFetch accepts the following arguments:
path: the server-side URL to fetch from. Corresponds to an HTTP route defined on in your backend Gadget app's routes folder
options: options configuring the fetch call, corresponding exactly to those you might send with a normal fetch.
method: the request method, like "GET", "POST", etc. Defaults to "GET"
headers: the request headers, like { "content-type": "application/json" }
body: the request body to send to the server, like "hello" or JSON.stringify({foo: "bar"})
json: If true, expects the response to be returned as JSON, and parses it for convenience
stream:
If true, response will be a ReadableStream object, allowing you to work with the response as it arrives
If "string", will decode the response as a string and update data as the response arrives; this is useful when streaming responses from LLMs
onStreamComplete: a callback function that will be called with the final content when the streaming response is complete; this is only available when the stream: "string" option is set
sendImmediately: If true, sends the first fetch on component mount. If false, waits for the send function to be called to send a request. Defaults to true for GET requests and false for any other HTTP verbs.
useFetch returns a tuple with the current state of the request and a function to send or re-send the request. The state is an object with the following fields:
data: the response data, if the request was successful
fetching: a boolean describing if the fetch request is currently in progress
streaming: a boolean describing if the fetch request is currently streaming. This is only set when the option { stream: "string" } is set
error: an error object if the request failed in any way
The second return value is a function for sending or resending the fetch request.
Here's an example user component that uses useFetch to make a request to a routes/users/GET-me.js backend Gadget route:
9if(!data)return<>No user found with id={props.id}</>;
10
11return<div>{data.name}</div>;
12}
Request method
By default, GET requests are sent as soon as the hook executes. GET requests can also be refreshed by calling the second return value to re-send the fetch request and fetch fresh data.
JavaScript
1// GET request will be sent immediately, can be refreshed by calling `refresh()` again
Other request methods like POST, DELETE, etc will not be sent automatically. The request will only be sent when the send functions is called explicitly, often in a click handler or similar.
JavaScript
// POST request will not be sent immediately, will only be sent the first time when `send()` is called
useFetch has a handy json: true option for automatically parsing a JSON response from the server. If you know your route will return JSON, you can set this option to true and the response will be parsed and returned as an object.
The @gadgetinc/react hooks library includes a useFetch hook, but if you'd like to use your preferred HTTP hook library, you can! By wrapping api.fetch with one of the great existing React libraries for making HTTP calls, like use-http, swr or react-query, you can continue passing the same authentication state and headers to your backend.
For example, we call a /example route in the backend with the swr library. First, create the route by adding the routes/GET-example.js file:
routes/GET-example.js
JavaScript
module.exports=async(request, reply)=>{
reply.send({message:"Hello from the backend!"});
};
Next, install swr into your application by adding the following to package.json and clicking the Run Yarn button:
package.json
json
{
"swr":"^2.0.4"
}
Then, in your React component, import the useSWR hook from swr and the api.fetch function from your Gadget API client:
Gadget's frontend hosting supports serving static assets in a robust, CDN-friendly way using Vite. You can add static assets anywhere within your frontend directory, and then import them into your code. For example, if you add an image at frontend/images/hero.png, you can import it in your frontend code like this:
React
importimgUrlfrom"./images/hero.png";
exportconstHero=()=>{
return<imgsrc={imgUrl}alt="a hero image"/>;
};
If you import from a static asset in your frontend code, Vite will take over serving this file with a robust production cache expiry mechanism, which will change the URL used to access that file in production.
Production asset links
When building assets for production, Vite will transform imported filenames to add a caching-friendly hash, and Gadget will upload these renamed files to Gadget's CDN for optimal serving. Your production Gadget app will make use of the hash generated by Vite to perform cache busting and will save and load assets to and from the disk cache between browser sessions. If you change these assets, Vite will generate a new hash and the updated asset will be loaded from Gadget's CDN on the next page render.
Non-transformed public assets
Gadget's frontend can host assets that aren't transformed by Vite, like your app's favicon or a robots.txt file. Instead of being minified and cached by vite, these files will be served as-is from your app's filesystem.
You can store these assets in a public folder at the root level of your Gadget project.
For example, if you have a public folder in your application like this:
public/
favicon.ico
robots.txt
foo/
bar.txt
You can access your robots.txt file by making a request to https://example-app--development.gadget.app/robots.txt.
The public folder also supports sub-folders. You can access the public/foo/bar.txt file by making a request to
https://example-app--development.gadget.app/foo/bar.txt.
Vite configuration
Gadget exposes the vite.config.js file that powers the Vite integration hosting your frontend. In vite.config.js, you can adjust the set of Vite plugins that power your application.
For example, we can add [MDX] support to a Gadget frontend with the @mdx-js/rollup plugin. First, install the plugin by adding the following to package.json and clicking the Run Yarn button:
package.json
json
{
"@mdx-js/rollup":"^2.3.0"
}
Then, in our vite.config.js, we can add the plugin to the list of plugins:
frontend/vite.config.js
JavaScript
1importreactfrom"@vitejs/plugin-react";
2importmdxfrom"@mdx-js/rollup";
3import{ defineConfig }from"vite";
4
5exportdefaultdefineConfig({
6plugins:[
7// newly added mdx plugin, configured as MDX recommends in their docs: https://mdxjs.com/docs/getting-started/#vite