Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
@preact-signals/query
Advanced tools
A reactive utility for React/Preact that simplifies the handling of data fetching and state management. Powered by Preact Signals, it provides hooks and functions to create reactive resources and manage their state seamlessly.
@preact-signals/query
@preact-signals/query
acts as a bridge between the core functionality of @tanstack/query-core
and the reactivity provided by @preact/signals
. Designed as a drop-in replacement for @tanstack/react-query
, this library not only mirrors its counterpart's functionalities but also offers enhanced hooks tailored for preact signals.
You should be sure that one of preact signals runtimes installed:
@preact/signals
for preact
, it requires additional step@preact/signals-react
for react
Fetch @preact-signals/query
via your preferred package manager:
# Using npm
npm install @preact-signals/query
# Using yarn
yarn add @preact-signals/query
# Using pnpm
pnpm add @preact-signals/query
@preact/signals
additional step:You should resolve @preact/signals-react
as @preact/signals
To do it take a look at how to resolve react
as preact
and do it with signals. Plus you need to dedupe preact
import preact from "@preact/preset-vite";
import { defineConfig } from "vite";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [preact()],
resolve: {
// add this line
dedupe: ["preact"],
alias: [
{ find: "react", replacement: "preact/compat" },
{ find: "react-dom/test-utils", replacement: "preact/test-utils" },
{ find: "react-dom", replacement: "preact/compat" },
{ find: "react/jsx-runtime", replacement: "preact/jsx-runtime" },
// add this line
{ find: "@preact/signals-react", replacement: "@preact/signals" },
],
},
});
Experience the reactive elegance of @tanstack/react-query
with @preact-signals/query
.
Although @preact-signals/query
adopts the API of @tanstack/react-query
, it comes with additional hooks that are specifically optimized for preact signals. You'll recognize these hooks by the appended $
sign:
useQuery$
, useInfiniteQuery$
useMutation$
useQueryClient$
useIsFetching$
Awaited hooks include:
useQueries$
useIsMutating$
useQuery$, useInfiniteQuery$
useQuery$
stands as the reactive counterpart to useQuery
from @tanstack/react-query
. Instead of the usual reactive object, this hook yields a flat-store.
Primary Differences:
options
that returns StaticQueryOptions
, as they're executed once initially and then reused when reactivity comes into play.suspense
and useErrorBoundary
are demand-triggered. They're invoked at the exact moment the data
field is accessed.onError
, onSettled
, and onSuccess
are phased out in react-query
, these aren't implemented in reactive query hooks.const isUserRegistered = useSignal(false);
const query = useQuery$(() => ({
queryKey: ["user"],
queryFn: () => fetchUser(),
enabled: isUserRegistered.value,
}));
return (
<>
<button onClick={() => (isUserRegistered.value = !isUserRegistered.value)}>
Toggle Registration
</button>
<Show when={() => query.data}>
{(data) => <div>Name: {data().name}</div>}
</Show>
</>
);
const query = useQuery$(() => ({
queryKey: ["key"],
queryFn: fetchStatistics,
suspense: true,
}));
return (
<>
<Profile />
<Jokes />
{/* Here, only this segment will enter suspense mode */}
<Suspense fallback={<Loader />}>
<Show when={() => query.data}>
{(data) => (
<ul>
{data().map((item) => (
<li key={item.label}>{item.data}</li>
))}
</ul>
)}
</Show>
</Suspense>
</>
);
useMutation$
Functionally similar to the query$ hooks, with a couple of nuances:
useErrorBoundary
isn't available. It's under evaluation for its utility.const mutation = useMutation$(() => ({
mutationFn: doSomething,
onError: (error) => {
console.error("doSomething failed", error);
},
onSuccess: (data) => {
console.log("wow we've done something", data);
},
}));
return <button onClick={mutation.mutate}>Execute Mutation</button>;
useQueryClient$
This hook returns the client, encapsulated in signals.
useIsFetching$
Accepts a reactive callback returning filter options and provides an accessor for the result.
// returns ReadonlySignal<number>
const overallFetching = useIsFetching$(() => null);
const specificFetchCount = useIsFetching$(() => ({
queryFn: ["123"],
}));
return (
<>
<div>Total fetching queries (unoptimized): {overallFetching.value}</div>
<div>
Total fetching queries (optimized, no rerenders): {overallFetching}
</div>
<div>
Fetch count by key (optimized, no rerenders): {specificFetchCount}
</div>
</>
);
@preact-signals/query
is distributed under the MIT License.
FAQs
A reactive utility for React/Preact that simplifies the handling of data fetching and state management. Powered by Preact Signals, it provides hooks and functions to create reactive resources and manage their state seamlessly.
The npm package @preact-signals/query receives a total of 207 weekly downloads. As such, @preact-signals/query popularity was classified as not popular.
We found that @preact-signals/query demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.