-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.d.ts
49 lines (45 loc) · 1.01 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import type { Store, StoreValue } from 'nanostores'
import type { DependencyList } from 'react'
type StoreKeys<T> = T extends { setKey: (k: infer K, v: any) => unknown }
? K
: never
export interface UseStoreOptions<SomeStore> {
/**
* @default
* ```ts
* [store, options.keys]
* ```
*/
deps?: DependencyList
/**
* Will re-render components only on specific key changes.
*/
keys?: StoreKeys<SomeStore>[]
}
/**
* Subscribe to store changes and get store’s value.
*
* Can be user with store builder too.
*
* ```js
* import { useStore } from 'nanostores/react'
*
* import { router } from '../store/router'
*
* export const Layout = () => {
* let page = useStore(router)
* if (page.route === 'home') {
* return <HomePage />
* } else {
* return <Error404 />
* }
* }
* ```
*
* @param store Store instance.
* @returns Store value.
*/
export function useStore<SomeStore extends Store>(
store: SomeStore,
options?: UseStoreOptions<SomeStore>
): StoreValue<SomeStore>