-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.d.ts
153 lines (143 loc) · 5.81 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* use-global-hook is built on top of React hooks, context and patterns surrounding those elements.
*
* It has four pieces:
* - hooks
* - createGlobalHook
* - useGlobalHook
* - `<GlobalHooksProvider />`
*/
import React, { FC } from 'react'
/**
* A hook store is a place to store state and some of the logic for updating it.
*
* Store is a very simple React hook (which means you can re-use it, use other
* hooks within it, use your existing custom or third-party hooks, etc).
*
* You should wrap the hook function in `createGlobalHook` function with the first parameter which is the unique identifier for the hook.
*
* Wrapping the function means, in case of when creating dynamic hook function, any argument you intend to pass to your hook when will be applied automatically, you still have your function the way you declare it --- cheers! e.g something like `counterStore(props.dynamicValue)` though this can only happen when registering the hook function in `<GlobalHooksProvider />`.
*
* ```tsx
* import { useState } from 'react'
* import { createGlobalHook } from '@devhammed/use-global-hook'
*
* const counterStore = createGlobalHook('counterStore', () => {
* const [count, setCount] = useState(0)
* const increment = () => setCount(count + 1)
* const decrement = () => setCount(count - 1)
*
* return { count, increment, decrement }
* })
* ```
*
* Note that hooks prop use useState hook from React for managing state
* When you call setState it triggers components to re-render, so be careful not
* to mutate state directly or your components won't re-render.
*/
export declare type StoreHook = () => any
/**
* Hook store function wrapper, this function apply some internally used property to a function that calls your original function. A wrapper function is best for this case as it is not a good idea to mutate your original function with properties that may conflict and third-party hooks is always taking into consideration where it is not good to add properties to the library core function and this method also allows creating clone of same hook function without conflicting names.
*/
export declare function createGlobalHook (
name: string,
fn: (...args) => any
): (...args) => any
/**
* Collection of Store Hooks
*/
export declare type StoreHooks = {
[key: string]: StoreHook
}
/**
* Global Store Hooks context container
*/
export declare const GlobalHooksContext: React.Context<StoreHooks>
export interface GlobalHooksProviderProps {
/**
* An object of hooks that will be available to retrieve with useGlobalHook
*/
hooks: StoreHooks
/**
* An array of React element children
*/
children: React.ReactChild[]
}
/**
* The `<GlobalHooksProvider>` component has two roles:
*
* - It initializes global instances of given hooks array (an Array is required because React expects the number of hooks to be consistent across re-renders and Objects are not guaranteed to return in same order)
* - It uses context to pass initialized instances of given hooks to all the components
* down the tree
*
* ```tsx
* ReactDOM.render(
* <GlobalHooksProvider hooks={[ counterStore ]}>
* <Counter />
* <Counter />
* </GlobalHooksProvider>
* )
* ```
*/
export declare function GlobalHooksProvider (
props: GlobalHooksProviderProps
): FC
/**
* Next we'll need a piece to introduce our state back into the tree so that:
*
* - When state changes, our components re-render.
* - We can depend on our store state.
* - We can call functions exposed by the store.
*
* For this we have the `useGlobalHook` hook which allows us to get global store instances by using passing the value we used when creating the global hook with `createGlobalHook` function.
*
* ```tsx
* function Counter() {
* const { count, decrement, increment } = useGlobalHook('counterStore')
*
* return (
* <div>
* <span>{count}</span>
* <button onClick={decrement}>-</button>
* <button onClick={increment}>+</button>
* </div>
* )
* }
* ```
*/
export declare function useGlobalHook (key: string): StoreHook
/**
* Class components can benefit from hooks too!
*
* You heard that right, who says class component cannot use and benefit from the awesomeness of React hooks?
use-global-hooks provides a function component HOC wrapper `withGlobalHooks` which allows class components to use hooks state(s) by passing them props. cool right? let's look at how above Counter component will look when using a class.
* It is as easy as using function component too, just pass in your component variable as the first parameter and second parameter is an array that contains names of the global hooks you want to use and you will be able access the state from `this.props.[globalHookName]`.
* The HOC wrapper pass down props so any other prop you are using in your component still works fine except if there is prop conflict which is why it is recommended you add `Store` suffix to your store names when creating them.
* So with support for class component, you can start using this library even when you are not ready to switch to function components.
* NOTE: You can also use `withGlobalHooks` with function components but why not just use the hook? :wink:
*
* ```tsx
* import { withGlobalHooks } from '@devhammed/use-global-hook'
*
* class Counter extends React.Component {
* render () {
* const { count, increment, decrement, reset } = this.props.counterStore
*
* return (
* <div>
* <button onClick={decrement}>-</button>
* <span>{count}</span>
* <button onClick={increment}>+</button>
* <button onClick={reset}>reset</button>
* </div>
* )
* }
* }
*
* export default withGlobalHooks(Counter, ['counterStore'])
* ```
*/
export declare function withGlobalHooks (
component: React.ReactElement,
hooks: string[]
): React.FunctionComponent