
Product
Introducing Webhook Events for Alert Changes
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.
@gadgetinc/preact
Advanced tools
<img alt="GitHub CI status" src="https://badgen.net/github/checks/ga
@gadgetinc/preact is a set of Preact hooks for connecting to a Gadget backend application from a Preact application. There's a few key features:
useFindOne and useActionselect optionDatesThis library wraps urql to communicate with a Gadget-generated API, so it benefits from all of
the same features as urql as well!
@gadgetinc/preact is built on top of @gadgetinc/client-hooks, which provides framework-agnostic hook implementations, and @gadgetinc/core, which defines the core types and interfaces for all Gadget clients.
@gadgetinc/preact provides a subset of the hooks available in @gadgetinc/react. This package focuses on core data fetching and mutation hooks. For advanced features like authentication components, form helpers (useActionForm), and other React-specific utilities, please use @gadgetinc/react.
The following hooks are available in @gadgetinc/preact:
useFindOne, useMaybeFindOne, useFindMany, useFindFirst, useMaybeFindFirst, useFindBy, useGetuseAction, useGlobalAction, useBulkActionuseEnqueueuseQuery (as useGadgetQuery), useMutation (as useGadgetMutation), useFetchuseView@gadgetinc/preact is a companion package to the JavaScript client package generated for your Gadget app, so you must install the JS client for your app, and then install this package.
To install the JS client for your app, you must set up the Gadget NPM registry, and then install the client:
npm config set @gadget-client:registry https://registry.gadget.dev/npm
# then
yarn add @gadget-client/example-app-slug
# or
npm install @gadget-client/example-app-slug
Full installation instructions can be found in the Gadget docs at https://docs.gadget.dev/api/<my-app-slug>/external-api-calls/installing.
Once you have your JS client installed, you can install the Preact hooks library with yarn or npm:
yarn add @gadgetinc/preact preact
# or
npm install --save @gadgetinc/preact preact
And finally, you must instantiate an instance of your API client, and then wrap your application in the Provider component exported from this library. In your root-most Preact component, you can add a Provider around the other components of your application:
// import the API client for your specific application from your client package, be sure to replace this package name with your own
import { Client } from "@gadget-client/example-app-slug";
// import the required Provider object and some example hooks from this package
import { Provider } from "@gadgetinc/preact";
// instantiate the API client for our app
const api = new Client({ authenticationMode: { browserSession: true } });
export const MyApp = (props) => {
// wrap the application in the <Provider> so the hooks can find the current client
return <Provider api={api}>{props.children}</Provider>;
};
// import the API client for your specific application from your client package, see your app's installing instructions
import { Client } from "@gadget-client/my-gadget-app";
// import the required Provider object and some example hooks from this package
import { Provider, useAction, useFindMany } from "@gadgetinc/preact";
// instantiate the API client for our app
const api = new Client({ authenticationMode: { browserSession: true } });
export function MyComponent() {
// ensure any components which use the @gadgetinc/preact hooks are wrapped with the provider, and passed the current api client
return (
<Provider api={api}>
<WidgetDeleter />
</Provider>
);
}
function WidgetDeleter() {
// `useFindMany` executes a backend fetch to get a list of widgets from the backend. we select only the id and name fields of each widget to demonstrate type-safe sub selection
const [result, refresh] = useFindMany(api.widget, {
select: {
id: true,
name: true,
},
});
// `useAction` sets up a mutation we can run to delete a specific widget when a user clicks a button
const [_, deleteWidget] = useAction(api.widget.delete);
// handle all the different states the data fetch can be in
if (result.error) return <>Error: {result.error.toString()}</>;
if (result.fetching && !result.data) return <>Fetching...</>;
if (!result.data) return <>No widgets found</>;
return (
<>
{result.data.map((widget) => (
<button
onClick={(event) => {
event.preventDefault();
void deleteWidget({ id: widget.id }).then(() => refresh());
}}
>
Delete {widget.name}
</button>
))}
</>
);
}
Find more examples in the https://github.com/gadget-inc/examples repo.
Under the hood, your Gadget app's API client and @gadgetinc/preact use a powerful, production-grade GraphQL client called urql. urql has a great client-side data caching feature built-in called Document Caching which allows Preact components issuing GraphQL requests for the same data to de-duplicate requests and share client-side state. @gadgetinc/preact enables this functionality by default.
@gadgetinc/preact runs urql's Document Caching with a default requestPolicy of cache-and-network, which means your Preact hooks will re-render data with any cached results from the in-memory store, and then make an underlying HTTP request to fetch the most up to date data.
If you want to change the default requestPolicy that your Gadget API client and Preact hooks use, you can pass the requestPolicy option to your API client constructor.
// instantiate the API client for our app that will make network calls for every query, regardless of cache state
const api = new Client({
requestPolicy: "network-only",
});
There are four different request policies that you can use:
cache-first prefers cached results and falls back to sending an API request when no prior result is cached.cache-and-network (the default) returns cached results but also always sends an API request, which is perfect for displaying data quickly while keeping it up-to-date.network-only will always send an API request and will ignore cached results.cache-only will always return cached results or null.For more information on urql's built-in client-side caching, see urql's docs.
@gadgetinc/preact is part of a layered package architecture:
@gadgetinc/core - Core types and interfaces that all Gadget clients must conform to. Defines AnyClient, GadgetRecord, filter types, etc.@gadgetinc/client-hooks - Framework-agnostic hook implementations that work across React, Preact, and other frameworks via a runtime adapter pattern.@gadgetinc/preact - Preact-specific bindings that provide the Provider component and re-export hooks from @gadgetinc/client-hooks configured for Preact.This architecture allows:
@gadgetinc/client-hooks and reused across frameworks@gadgetinc/core@gadgetinc/client-hooksWhen using @gadgetinc/preact, you only need to install this package - it automatically includes the necessary dependencies.
@gadgetinc/preact contains a variety of Preact hooks for working with your Gadget application's API from a Preact application. Specific code examples for your application's API can be found within your application's docs at https://docs.gadget.dev/api/
Your Preact application must be wrapped in the Provider component from this library for the hooks to function properly. No other wrappers (like urql's) are necessary.
The following hooks are available in @gadgetinc/preact. For detailed documentation on each hook's usage, options, and return values, please refer to the @gadgetinc/react documentation, as the APIs are identical:
useFindOne(manager, id, options) - Fetch one record by IDuseMaybeFindOne(manager, id, options) - Fetch one record by ID, returns null if not founduseFindMany(manager, options) - Fetch a page of records with filtering, sorting, and paginationuseFindFirst(manager, options) - Fetch the first record matching conditionsuseMaybeFindFirst(manager, options) - Fetch the first record, returns null if not founduseFindBy(findFunction, fieldValue, options) - Fetch one record by a unique field valueuseGet(singletonModelManager, options) - Fetch a singleton record (e.g., api.currentSession)useAction(actionFunction, options) - Run a model action (create, update, delete, custom actions)useGlobalAction(globalActionFunction, options) - Run a global actionuseBulkAction(bulkActionFunction, options) - Run a bulk action on multiple recordsuseEnqueue(actionFunction, options) - Enqueue a background action for async executionuseView(viewFunction, variables, options) - Query custom Gelly viewsuseGadgetQuery(options) - Run a custom GraphQL query (alias for urql's useQuery)useGadgetMutation(options) - Run a custom GraphQL mutation (alias for urql's useMutation)useFetch(path, options) - Make HTTP requests to your Gadget backend's custom routesselect optionThe select option allows you to choose which fields and subfields are returned by your Gadget app's GraphQL API. Your app's API supports returning only some fields of each model you request, as well as fields of related models through the Gadget relationship field types.
export const OnlySomeWidgetFields = (props) => {
// fetch only the widget id and name fields
const [{ data, fetching, error }, _refetch] = useFindOne(api.widget, props.id, { select: { id: true, name: true } });
return (
<span className="widget-name">
{data?.id}: {data?.name}
</span>
);
};
refetch functionMost read hooks return a refetch function as the second element of the returned array. This function can be called to re-fetch the most up-to-date data from the backend.
const [{ data, fetching, error }, refetch] = useFindMany(api.widget);
// Later, to refresh the data:
void refetch();
All hooks return an error object that contains detailed information about any errors that occurred:
error.message: string - A top level error messageerror.networkError: Error | undefined - Browser network errorserror.executionErrors: (GraphQLError | GadgetError)[] | undefined - GraphQL API errorserror.validationErrors: { apiIdentifier: string, message: string }[] | undefined - Validation errors from the backendData reading hooks support Preact's Suspense for declarative loading states. Pass suspense: true to any read hook:
const Posts = () => {
// Component will suspend while data is loading
const [{ data, error }, refresh] = useFindMany(api.post, { suspense: true });
// No need to check fetching state
return (
<>
{data.map((post) => (
<Post key={post.id} post={post} />
))}
</>
);
};
// Wrap with Suspense boundary
<Suspense fallback={<div>Loading...</div>}>
<Posts />
</Suspense>;
Supported on: useFindOne, useMaybeFindOne, useFindMany, useFindFirst, useMaybeFindFirst, useFindBy, useGet, and useView.
@gadgetinc/reactWhile @gadgetinc/preact and @gadgetinc/react share the same core data hooks, @gadgetinc/preact does not include:
<SignedIn>, <SignedOut>, <SignedInOrRedirect>, <SignedOutOrRedirect>)useAuth, useUser, useSession, useSignOut)useActionForm hook for form state managementIf you need these features, please use @gadgetinc/react instead. The core data hooks work identically in both packages.
@gadgetinc/preact is particularly useful for Shopify applications where bundle size is critical. Preact's smaller footprint makes it ideal for embedded apps and Shopify extensions while maintaining full compatibility with the Gadget API.
@gadgetinc/preact is fully typed and benefits from type generation in your Gadget app's API client. All hooks provide full type safety for query inputs, mutation variables, and returned data.
MIT
Contributions are welcome! Please see the main repository at https://github.com/gadget-inc/js-clients for contribution guidelines.
FAQs
<img alt="GitHub CI status" src="https://badgen.net/github/checks/ga
We found that @gadgetinc/preact demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.

Product
Socket now scans OpenVSX extensions, giving teams early detection of risky behaviors, hidden capabilities, and supply chain threats in developer tools.