Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
nano-query-2
Advanced tools
Nano Query is a minimalistic and type strict library for creating custom query and mutate hooks in React. It leverages the power of nanostores, axios, and zod to provide a simple and efficient way to manage API requests and state in your application.
Nano Query is a minimalistic and type strict library for creating custom query and mutate hooks in React. It leverages the power of nanostores, axios, and zod to provide a simple and efficient way to manage API requests and state in your application.
You can install Nano Query via npm:
npm install nano-query-2
First, import the createQueryHook
function from Nano Query:
import { createQueryHook } from "nano-query-2";
Then, define a URL and a DTO (Data Transfer Object) using zod:
import { z } from "zod"
// Define the URL for the API endpoint, with a parameter placeholder
const url = '/test/:myPathParam'
// Define the structure of the response data using zod for validation
const dto = z.object({
name: z.string(),
})
Finally, create the query hook:
// Create a custom query hook using the createQueryHook function
const useQuery = createQueryHook(url, {
// Default validator of the response body
defaultValidator: dto,
// List of nanostores atoms to watch and trigger refetch when they change
watchAtoms: [],
// Additional axios request config options can be provided here
// ...
})
Now you can use the query hook in your components:
import React, { useEffect } from "react"
// Define a test hook to use the custom query hook
export const testHook = () => {
// Create memoized params for useQuery to avoid unnecessary rerenders
const params = React.useMemo(() => ({
pathParams: {
myPathParam: 'test' // Define path parameters for the request
},
// This option disables the query hook until it becomes false
skip: false,
// Additional axios request config options can be provided here
}), [])
// Use the custom query hook with the memoized params
const queryResult = useQuery(params)
// Perform an action with the query result
useEffect(() => {
// If the request was successful, log the name from the response data
if (queryResult.isSuccess) {
console.log(queryResult.data.name)
}
}, [queryResult])
}
Similar to the query hook, you can create a mutation hook:
import { createMutateHook } from "nano-query-2";
// Create a custom mutate hook using the createMutateHook function
const useMutate = createMutateHook(url, {
// Default validator of the response body
defaultValidator: dto,
// List of nanostores atoms to trigger query hook when they change to update them
emitAtoms: [],
// Additional axios request config options can be provided here
})
And use it in your components:
// Define a test hook to use the custom mutate hook
export const testMutate = () => {
// Create memoized params for useQuery to avoid unnecessary rerenders
const params = React.useMemo(() => ({
// Additional axios request config options can be provided here
}), [])
// Use the custom mutate hook with the memoized params
const [{ cancel, mutate, reset }, queryResult] = useMutate(params)
useEffect(() => {
// Trigger a mutation with the specified path parameters
mutate({
pathParams: {
myPathParam: 'test' // Define path parameters for the mutation
},
})
}, [])
// Perform an action with the mutation result
useEffect(() => {
// If the request was successful, log the name from the response data
if (queryResult.isSuccess) {
console.log(queryResult.data.name)
}
}, [queryResult])
}
Pull requests are welcome. Please make sure to update tests as appropriate.
This project is licensed under the MIT License.
FAQs
Nano Query is a minimalistic and type strict library for creating custom query and mutate hooks in React. It leverages the power of nanostores, axios, and zod to provide a simple and efficient way to manage API requests and state in your application.
The npm package nano-query-2 receives a total of 0 weekly downloads. As such, nano-query-2 popularity was classified as not popular.
We found that nano-query-2 demonstrated a not healthy version release cadence and project activity because the last version was released 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.