@nanostores/query
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -25,2 +25,3 @@ import { map, onStart, onStop, atom } from "nanostores"; | ||
store.set(v); | ||
events.emit(SET_KEY, key, v); | ||
} | ||
@@ -141,2 +142,6 @@ }, setKey = (k, v) => { | ||
} | ||
}), | ||
events.on(SET_KEY, (key, value) => { | ||
if (key === prevKey && fetcherStore.value !== value) | ||
fetcherStore.set(value); | ||
}) | ||
@@ -257,3 +262,3 @@ ); | ||
}; | ||
const FOCUS = 1, RECONNECT = 2, INVALIDATE_KEYS = 3, MUTATE_CACHE = 4; | ||
const FOCUS = 1, RECONNECT = 2, INVALIDATE_KEYS = 3, MUTATE_CACHE = 4, SET_KEY = 5; | ||
const subscribe = (name, fn) => { | ||
@@ -260,0 +265,0 @@ const isServer = typeof window === "undefined"; |
{ | ||
"name": "@nanostores/query", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Tiny remote data fetching library for Nano Stores", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -170,1 +170,27 @@ # Nano Stores Query | ||
``` | ||
## Recipes | ||
### Local state and Pagination | ||
All examples above use module-scoped stores, therefore they can only have a single | ||
data point stored. But what if you need, say, a store that fetches data based on | ||
component state? Nano Stores do not limit you in any way, you can easily achieve | ||
this by creating a store instance limited to a single component: | ||
```tsx | ||
const createStore = (id: string) => () => | ||
createFetcherStore<{ avatarUrl: string }>(`/api/user/${id}`); | ||
const UserAvatar: FC<{ id: string }> = ({ id }) => { | ||
const [$user] = useState(createStore(id)); | ||
const { data } = useStore($user); | ||
if (!data) return null; | ||
return <img src={data.avatarUrl} />; | ||
}; | ||
``` | ||
This way you can leverage all nanoquery features, like cache or refetching, but | ||
not give up the flexibility of component-level data fetching. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29522
612
196