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 | import {
|
27 | ClassAttributes,
|
28 | Component,
|
29 | ComponentClass,
|
30 | ComponentType,
|
31 | StatelessComponent,
|
32 | Context,
|
33 | NamedExoticComponent
|
34 | } from 'react';
|
35 |
|
36 | import {
|
37 | Action,
|
38 | ActionCreator,
|
39 | AnyAction,
|
40 | Dispatch,
|
41 | Store
|
42 | } from 'redux';
|
43 |
|
44 | import hoistNonReactStatics = require('hoist-non-react-statics');
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | export interface DefaultRootState {}
|
54 |
|
55 | export type AnyIfEmpty<T extends object> = keyof T extends never ? any : T;
|
56 | export type RootStateOrAny = AnyIfEmpty<DefaultRootState>;
|
57 |
|
58 |
|
59 | export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
|
60 |
|
61 | export type DistributiveOmit<T, K extends keyof T> = T extends unknown ? Omit<T, K> : never;
|
62 |
|
63 | export interface DispatchProp<A extends Action = AnyAction> {
|
64 | dispatch: Dispatch<A>;
|
65 | }
|
66 |
|
67 | export type AdvancedComponentDecorator<TProps, TOwnProps> =
|
68 | (component: ComponentType<TProps>) => NamedExoticComponent<TOwnProps>;
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | export type Matching<InjectedProps, DecorationTargetProps> = {
|
83 | [P in keyof DecorationTargetProps]: P extends keyof InjectedProps
|
84 | ? InjectedProps[P] extends DecorationTargetProps[P]
|
85 | ? DecorationTargetProps[P]
|
86 | : InjectedProps[P]
|
87 | : DecorationTargetProps[P];
|
88 | };
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | export type Shared<
|
101 | InjectedProps,
|
102 | DecorationTargetProps
|
103 | > = {
|
104 | [P in Extract<keyof InjectedProps, keyof DecorationTargetProps>]?: InjectedProps[P] extends DecorationTargetProps[P] ? DecorationTargetProps[P] : never;
|
105 | };
|
106 |
|
107 |
|
108 | export type GetProps<C> = C extends ComponentType<infer P>
|
109 | ? C extends ComponentClass<P> ? ClassAttributes<InstanceType<C>> & P : P
|
110 | : never;
|
111 |
|
112 |
|
113 |
|
114 | export type GetLibraryManagedProps<C> = JSX.LibraryManagedAttributes<C, GetProps<C>>;
|
115 |
|
116 |
|
117 | export type ConnectedComponent<
|
118 | C extends ComponentType<any>,
|
119 | P
|
120 | > = NamedExoticComponent<P> & hoistNonReactStatics.NonReactStatics<C> & {
|
121 | WrappedComponent: C;
|
122 | };
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | export type InferableComponentEnhancerWithProps<TInjectedProps, TNeedsProps> =
|
129 | <C extends ComponentType<Matching<TInjectedProps, GetProps<C>>>>(
|
130 | component: C
|
131 | ) => ConnectedComponent<C, DistributiveOmit<GetLibraryManagedProps<C>, keyof Shared<TInjectedProps, GetLibraryManagedProps<C>>> & TNeedsProps>;
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | export type InferableComponentEnhancer<TInjectedProps> =
|
137 | InferableComponentEnhancerWithProps<TInjectedProps, {}>;
|
138 |
|
139 | export type InferThunkActionCreatorType<TActionCreator extends (...args: any[]) => any> =
|
140 | TActionCreator extends (...args: infer TParams) => (...args: any[]) => infer TReturn
|
141 | ? (...args: TParams) => TReturn
|
142 | : TActionCreator;
|
143 |
|
144 | export type HandleThunkActionCreator<TActionCreator> =
|
145 | TActionCreator extends (...args: any[]) => any
|
146 | ? InferThunkActionCreatorType<TActionCreator>
|
147 | : TActionCreator;
|
148 |
|
149 |
|
150 |
|
151 | export type ResolveThunks<TDispatchProps> =
|
152 | TDispatchProps extends { [key: string]: any }
|
153 | ? {
|
154 | [C in keyof TDispatchProps]: HandleThunkActionCreator<TDispatchProps[C]>
|
155 | }
|
156 | : TDispatchProps;
|
157 |
|
158 |
|
159 |
|
160 | export type ResolveArrayThunks<TDispatchProps extends ReadonlyArray<any>> =
|
161 | TDispatchProps extends [infer A1, infer A2, infer A3, infer A4, infer A5, infer A6, infer A7, infer A8, infer A9]
|
162 | ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>, HandleThunkActionCreator<A3>, HandleThunkActionCreator<A4>,
|
163 | HandleThunkActionCreator<A5>, HandleThunkActionCreator<A6>, HandleThunkActionCreator<A7>, HandleThunkActionCreator<A8>, HandleThunkActionCreator<A9>]
|
164 | : TDispatchProps extends [infer A1, infer A2, infer A3, infer A4, infer A5, infer A6, infer A7, infer A8]
|
165 | ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>, HandleThunkActionCreator<A3>, HandleThunkActionCreator<A4>,
|
166 | HandleThunkActionCreator<A5>, HandleThunkActionCreator<A6>, HandleThunkActionCreator<A7>, HandleThunkActionCreator<A8>]
|
167 | : TDispatchProps extends [infer A1, infer A2, infer A3, infer A4, infer A5, infer A6, infer A7]
|
168 | ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>, HandleThunkActionCreator<A3>, HandleThunkActionCreator<A4>,
|
169 | HandleThunkActionCreator<A5>, HandleThunkActionCreator<A6>, HandleThunkActionCreator<A7>]
|
170 | : TDispatchProps extends [infer A1, infer A2, infer A3, infer A4, infer A5, infer A6]
|
171 | ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>, HandleThunkActionCreator<A3>, HandleThunkActionCreator<A4>, HandleThunkActionCreator<A5>, HandleThunkActionCreator<A6>]
|
172 | : TDispatchProps extends [infer A1, infer A2, infer A3, infer A4, infer A5]
|
173 | ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>, HandleThunkActionCreator<A3>, HandleThunkActionCreator<A4>, HandleThunkActionCreator<A5>]
|
174 | : TDispatchProps extends [infer A1, infer A2, infer A3, infer A4] ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>, HandleThunkActionCreator<A3>, HandleThunkActionCreator<A4>]
|
175 | : TDispatchProps extends [infer A1, infer A2, infer A3] ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>, HandleThunkActionCreator<A3>]
|
176 | : TDispatchProps extends [infer A1, infer A2] ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>]
|
177 | : TDispatchProps extends [infer A1] ? [HandleThunkActionCreator<A1>]
|
178 | : TDispatchProps extends Array<infer A> ? Array<HandleThunkActionCreator<A>>
|
179 | : TDispatchProps extends ReadonlyArray<infer A> ? ReadonlyArray<HandleThunkActionCreator<A>>
|
180 | : never
|
181 | ;
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | export interface Connect<DefaultState = DefaultRootState> {
|
203 |
|
204 | (): InferableComponentEnhancer<DispatchProp>;
|
205 |
|
206 | <TStateProps = {}, no_dispatch = {}, TOwnProps = {}, State = DefaultState>(
|
207 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>
|
208 | ): InferableComponentEnhancerWithProps<TStateProps & DispatchProp, TOwnProps>;
|
209 |
|
210 | <no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
|
211 | mapStateToProps: null | undefined,
|
212 | mapDispatchToProps: MapDispatchToPropsNonObject<TDispatchProps, TOwnProps>
|
213 | ): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;
|
214 |
|
215 | <no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
|
216 | mapStateToProps: null | undefined,
|
217 | mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
|
218 | ): InferableComponentEnhancerWithProps<
|
219 | ResolveThunks<TDispatchProps>,
|
220 | TOwnProps
|
221 | >;
|
222 |
|
223 | <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = DefaultState>(
|
224 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
225 | mapDispatchToProps: MapDispatchToPropsNonObject<TDispatchProps, TOwnProps>
|
226 | ): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;
|
227 |
|
228 | <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = DefaultState>(
|
229 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
230 | mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
|
231 | ): InferableComponentEnhancerWithProps<
|
232 | TStateProps & ResolveThunks<TDispatchProps>,
|
233 | TOwnProps
|
234 | >;
|
235 |
|
236 | <no_state = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}>(
|
237 | mapStateToProps: null | undefined,
|
238 | mapDispatchToProps: null | undefined,
|
239 | mergeProps: MergeProps<undefined, undefined, TOwnProps, TMergedProps>,
|
240 | ): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
|
241 |
|
242 | <TStateProps = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}, State = DefaultState>(
|
243 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
244 | mapDispatchToProps: null | undefined,
|
245 | mergeProps: MergeProps<TStateProps, undefined, TOwnProps, TMergedProps>,
|
246 | ): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
|
247 |
|
248 | <no_state = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
|
249 | mapStateToProps: null | undefined,
|
250 | mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
|
251 | mergeProps: MergeProps<undefined, TDispatchProps, TOwnProps, TMergedProps>,
|
252 | ): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
|
253 |
|
254 | <TStateProps = {}, no_dispatch = {}, TOwnProps = {}, State = DefaultState>(
|
255 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
256 | mapDispatchToProps: null | undefined,
|
257 | mergeProps: null | undefined,
|
258 | options: Options<State, TStateProps, TOwnProps>
|
259 | ): InferableComponentEnhancerWithProps<DispatchProp & TStateProps, TOwnProps>;
|
260 |
|
261 | <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
|
262 | mapStateToProps: null | undefined,
|
263 | mapDispatchToProps: MapDispatchToPropsNonObject<TDispatchProps, TOwnProps>,
|
264 | mergeProps: null | undefined,
|
265 | options: Options<{}, TStateProps, TOwnProps>
|
266 | ): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;
|
267 |
|
268 | <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
|
269 | mapStateToProps: null | undefined,
|
270 | mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
|
271 | mergeProps: null | undefined,
|
272 | options: Options<{}, TStateProps, TOwnProps>
|
273 | ): InferableComponentEnhancerWithProps<
|
274 | ResolveThunks<TDispatchProps>,
|
275 | TOwnProps
|
276 | >;
|
277 |
|
278 | <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = DefaultState>(
|
279 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
280 | mapDispatchToProps: MapDispatchToPropsNonObject<TDispatchProps, TOwnProps>,
|
281 | mergeProps: null | undefined,
|
282 | options: Options<State, TStateProps, TOwnProps>
|
283 | ): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;
|
284 |
|
285 | <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = DefaultState>(
|
286 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
287 | mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
|
288 | mergeProps: null | undefined,
|
289 | options: Options<State, TStateProps, TOwnProps>
|
290 | ): InferableComponentEnhancerWithProps<
|
291 | TStateProps & ResolveThunks<TDispatchProps>,
|
292 | TOwnProps
|
293 | >;
|
294 |
|
295 | <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}, State = DefaultState>(
|
296 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
297 | mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
|
298 | mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
|
299 | options?: Options<State, TStateProps, TOwnProps, TMergedProps>
|
300 | ): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
|
301 |
|
302 | }
|
303 |
|
304 |
|
305 |
|
306 |
|
307 | export type ConnectedProps<TConnector> =
|
308 | TConnector extends InferableComponentEnhancerWithProps<infer TInjectedProps, any>
|
309 | ? unknown extends TInjectedProps
|
310 | ? TConnector extends InferableComponentEnhancer<infer TInjectedProps>
|
311 | ? TInjectedProps
|
312 | : never
|
313 | : TInjectedProps
|
314 | : never;
|
315 |
|
316 |
|
317 |
|
318 |
|
319 | export const connect: Connect;
|
320 |
|
321 | export type MapStateToProps<TStateProps, TOwnProps, State = DefaultRootState> =
|
322 | (state: State, ownProps: TOwnProps) => TStateProps;
|
323 |
|
324 | export type MapStateToPropsFactory<TStateProps, TOwnProps, State = DefaultRootState> =
|
325 | (initialState: State, ownProps: TOwnProps) => MapStateToProps<TStateProps, TOwnProps, State>;
|
326 |
|
327 | export type MapStateToPropsParam<TStateProps, TOwnProps, State = DefaultRootState> =
|
328 | MapStateToPropsFactory<TStateProps, TOwnProps, State> | MapStateToProps<TStateProps, TOwnProps, State> | null | undefined;
|
329 |
|
330 | export type MapDispatchToPropsFunction<TDispatchProps, TOwnProps> =
|
331 | (dispatch: Dispatch<Action>, ownProps: TOwnProps) => TDispatchProps;
|
332 |
|
333 | export type MapDispatchToProps<TDispatchProps, TOwnProps> =
|
334 | MapDispatchToPropsFunction<TDispatchProps, TOwnProps> | TDispatchProps;
|
335 |
|
336 | export type MapDispatchToPropsFactory<TDispatchProps, TOwnProps> =
|
337 | (dispatch: Dispatch<Action>, ownProps: TOwnProps) => MapDispatchToPropsFunction<TDispatchProps, TOwnProps>;
|
338 |
|
339 | export type MapDispatchToPropsParam<TDispatchProps, TOwnProps> = MapDispatchToPropsFactory<TDispatchProps, TOwnProps> | MapDispatchToProps<TDispatchProps, TOwnProps>;
|
340 |
|
341 | export type MapDispatchToPropsNonObject<TDispatchProps, TOwnProps> = MapDispatchToPropsFactory<TDispatchProps, TOwnProps> | MapDispatchToPropsFunction<TDispatchProps, TOwnProps>;
|
342 |
|
343 | export type MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps> =
|
344 | (stateProps: TStateProps, dispatchProps: TDispatchProps, ownProps: TOwnProps) => TMergedProps;
|
345 |
|
346 | export interface Options<State = DefaultRootState, TStateProps = {}, TOwnProps = {}, TMergedProps = {}> extends ConnectOptions {
|
347 | |
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 | pure?: boolean | undefined;
|
355 |
|
356 | |
357 |
|
358 |
|
359 |
|
360 | areStatesEqual?: ((nextState: State, prevState: State) => boolean) | undefined;
|
361 |
|
362 | /**
|
363 | * When pure, compares incoming props to its previous value.
|
364 | * @default shallowEqual
|
365 | */
|
366 | areOwnPropsEqual?: ((nextOwnProps: TOwnProps, prevOwnProps: TOwnProps) => boolean) | undefined;
|
367 |
|
368 | /**
|
369 | * When pure, compares the result of mapStateToProps to its previous value.
|
370 | * @default shallowEqual
|
371 | */
|
372 | areStatePropsEqual?: ((nextStateProps: TStateProps, prevStateProps: TStateProps) => boolean) | undefined;
|
373 |
|
374 | /**
|
375 | * When pure, compares the result of mergeProps to its previous value.
|
376 | * @default shallowEqual
|
377 | */
|
378 | areMergedPropsEqual?: ((nextMergedProps: TMergedProps, prevMergedProps: TMergedProps) => boolean) | undefined;
|
379 |
|
380 | /**
|
381 | * If true, use React's forwardRef to expose a ref of the wrapped component
|
382 | *
|
383 | * @default false
|
384 | */
|
385 | forwardRef?: boolean | undefined;
|
386 | }
|
387 |
|
388 | /**
|
389 | * Connects a React component to a Redux store. It is the base for {@link connect} but is less opinionated about
|
390 | * how to combine <code>state</code>, <code>props</code>, and <code>dispatch</code> into your final props. It makes no
|
391 | * assumptions about defaults or memoization of results, leaving those responsibilities to the caller.It does not
|
392 | * modify the component class passed to it; instead, it returns a new, connected component for you to use.
|
393 | *
|
394 | * @param selectorFactory The selector factory. See SelectorFactory type for details.
|
395 | * @param connectOptions If specified, further customizes the behavior of the connector. Additionally, any extra
|
396 | * options will be passed through to your <code>selectorFactory</code> in the <code>factoryOptions</code> argument.
|
397 | */
|
398 | export function connectAdvanced<S, TProps, TOwnProps, TFactoryOptions = {}>(
|
399 |
|
400 | selectorFactory: SelectorFactory<S, TProps, TOwnProps, TFactoryOptions>,
|
401 | connectOptions?: ConnectOptions & TFactoryOptions
|
402 | ): AdvancedComponentDecorator<TProps, TOwnProps>;
|
403 |
|
404 | /**
|
405 | * Initializes a selector function (during each instance's constructor). That selector function is called any time the
|
406 | * connector component needs to compute new props, as a result of a store state change or receiving new props. The
|
407 | * result of <code>selector</code> is expected to be a plain object, which is passed as the props to the wrapped
|
408 | * component. If a consecutive call to <code>selector</code> returns the same object (<code>===</code>) as its previous
|
409 | * call, the component will not be re-rendered. It's the responsibility of <code>selector</code> to return that
|
410 | * previous object when appropriate.
|
411 | */
|
412 | export type SelectorFactory<S, TProps, TOwnProps, TFactoryOptions> =
|
413 | (dispatch: Dispatch<Action>, factoryOptions: TFactoryOptions) => Selector<S, TProps, TOwnProps>;
|
414 |
|
415 | export type Selector<S, TProps, TOwnProps = null> = TOwnProps extends null | undefined
|
416 | ? (state: S) => TProps
|
417 | : (state: S, ownProps: TOwnProps) => TProps;
|
418 |
|
419 | export interface ConnectOptions {
|
420 | |
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 | getDisplayName?: ((componentName: string) => string) | undefined;
|
428 | /**
|
429 | * Shown in error messages. Usually overridden by wrapper functions.
|
430 | *
|
431 | * @default 'connectAdvanced'
|
432 | */
|
433 | methodName?: string | undefined;
|
434 | /**
|
435 | * If defined, a property named this value will be added to the props passed to the wrapped component. Its value
|
436 | * will be the number of times the component has been rendered, which can be useful for tracking down unnecessary
|
437 | * re-renders.
|
438 | *
|
439 | * @default undefined
|
440 | */
|
441 | renderCountProp?: string | undefined;
|
442 | /**
|
443 | * Controls whether the connector component subscribes to redux store state changes. If set to false, it will only
|
444 | * re-render on <code>componentWillReceiveProps</code>.
|
445 | *
|
446 | * @default true
|
447 | */
|
448 | shouldHandleStateChanges?: boolean | undefined;
|
449 | /**
|
450 | * The key of props/context to get the store. You probably only need this if you are in the inadvisable position of
|
451 | * having multiple stores.
|
452 | *
|
453 | * @default 'store'
|
454 | */
|
455 | storeKey?: string | undefined;
|
456 | /**
|
457 | * @deprecated Use forwardRef
|
458 | *
|
459 | * @default false
|
460 | */
|
461 | withRef?: boolean | undefined;
|
462 | /**
|
463 | * The react context to get the store from.
|
464 | *
|
465 | * @default ReactReduxContext
|
466 | */
|
467 | context?: Context<ReactReduxContextValue> | undefined;
|
468 | }
|
469 |
|
470 | export interface ReactReduxContextValue<SS = any, A extends Action = AnyAction> {
|
471 | store: Store<SS, A>;
|
472 | storeState: SS;
|
473 | }
|
474 |
|
475 | export interface ProviderProps<A extends Action = AnyAction> {
|
476 | /**
|
477 | * The single Redux store in your application.
|
478 | */
|
479 | store: Store<any, A>;
|
480 | /**
|
481 | * Optional context to be used internally in react-redux. Use React.createContext() to create a context to be used.
|
482 | * If this is used, generate own connect HOC by using connectAdvanced, supplying the same context provided to the
|
483 | * Provider. Initial value doesn't matter, as it is overwritten with the internal state of Provider.
|
484 | */
|
485 | context?: Context<ReactReduxContextValue> | undefined;
|
486 | }
|
487 |
|
488 | /**
|
489 | * Makes the Redux store available to the connect() calls in the component hierarchy below.
|
490 | */
|
491 | export class Provider<A extends Action = AnyAction> extends Component<ProviderProps<A>> { }
|
492 |
|
493 | /**
|
494 | * Exposes the internal context used in react-redux. It is generally advised to use the connect HOC to connect to the
|
495 | * redux store instead of this approach.
|
496 | */
|
497 | export const ReactReduxContext: Context<ReactReduxContextValue>;
|
498 |
|
499 | /**
|
500 | * Wraps ReactDOM or React Native's internal unstable_batchedUpdate function. You can use it to ensure that
|
501 | * multiple actions dispatched outside of React only result in a single render update.
|
502 | */
|
503 | export function batch(cb: () => void): void;
|
504 |
|
505 | // tslint:disable:no-unnecessary-generics
|
506 |
|
507 | /**
|
508 | * Compares two arbitrary values for shallow equality. Object values are compared based on their keys, i.e. they must
|
509 | * have the same keys and for each key the value must be equal according to the `Object.is()` algorithm. Non-object
|
510 | * values are also compared with the same algorithm as `Object.is()`.
|
511 | */
|
512 | export function shallowEqual<T>(left: T, right: any): boolean;
|
513 |
|
514 | /**
|
515 | * A hook to access the redux `dispatch` function.
|
516 | *
|
517 | * Note for `redux-thunk` users: the return type of the returned `dispatch` functions for thunks is incorrect.
|
518 | * However, it is possible to get a correctly typed `dispatch` function by creating your own custom hook typed
|
519 | * from the store's dispatch function like this: `const useThunkDispatch = () => useDispatch<typeof store.dispatch>();`
|
520 | *
|
521 | * @returns redux store's `dispatch` function
|
522 | *
|
523 | * @example
|
524 | *
|
525 | * import React from 'react'
|
526 | * import { useDispatch } from 'react-redux'
|
527 | *
|
528 | * export const CounterComponent = ({ value }) => {
|
529 | * const dispatch = useDispatch()
|
530 | * return (
|
531 | * <div>
|
532 | * <span>{value}</span>
|
533 | * <button onClick={() => dispatch({ type: 'increase-counter' })}>
|
534 | * Increase counter
|
535 | * </button>
|
536 | * </div>
|
537 | * )
|
538 | * }
|
539 | */
|
540 | // NOTE: the first overload below and note above can be removed if redux-thunk typings add an overload for
|
541 | // the Dispatch function (see also this PR: https://github.com/reduxjs/redux-thunk/pull/247)
|
542 | export function useDispatch<TDispatch = Dispatch<any>>(): TDispatch;
|
543 | export function useDispatch<A extends Action = AnyAction>(): Dispatch<A>;
|
544 |
|
545 | /**
|
546 | * A hook to access the redux store's state. This hook takes a selector function
|
547 | * as an argument. The selector is called with the store state.
|
548 | *
|
549 | * This hook takes an optional equality comparison function as the second parameter
|
550 | * that allows you to customize the way the selected state is compared to determine
|
551 | * whether the component needs to be re-rendered.
|
552 | *
|
553 | * If you do not want to have to specify the root state type for whenever you use
|
554 | * this hook with an inline selector you can use the `TypedUseSelectorHook` interface
|
555 | * to create a version of this hook that is properly typed for your root state.
|
556 | *
|
557 | * @param selector the selector function
|
558 | * @param equalityFn the function that will be used to determine equality
|
559 | *
|
560 | * @returns the selected state
|
561 | *
|
562 | * @example
|
563 | *
|
564 | * import React from 'react'
|
565 | * import { useSelector } from 'react-redux'
|
566 | * import { RootState } from './store'
|
567 | *
|
568 | * export const CounterComponent = () => {
|
569 | * const counter = useSelector((state: RootState) => state.counter)
|
570 | * return <div>{counter}</div>
|
571 | * }
|
572 | */
|
573 | export function useSelector<TState = DefaultRootState, TSelected = unknown>(
|
574 | selector: (state: TState) => TSelected,
|
575 | equalityFn?: (left: TSelected, right: TSelected) => boolean
|
576 | ): TSelected;
|
577 |
|
578 | /**
|
579 | * This interface allows you to easily create a hook that is properly typed for your
|
580 | * store's root state.
|
581 | *
|
582 | * @example
|
583 | *
|
584 | * interface RootState {
|
585 | * property: string;
|
586 | * }
|
587 | *
|
588 | * const useTypedSelector: TypedUseSelectorHook<RootState> = useSelector;
|
589 | */
|
590 | export interface TypedUseSelectorHook<TState> {
|
591 | <TSelected>(
|
592 | selector: (state: TState) => TSelected,
|
593 | equalityFn?: (left: TSelected, right: TSelected) => boolean
|
594 | ): TSelected;
|
595 | }
|
596 |
|
597 | /**
|
598 | * A hook to access the redux store.
|
599 | *
|
600 | * @returns the redux store
|
601 | *
|
602 | * @example
|
603 | *
|
604 | * import React from 'react'
|
605 | * import { useStore } from 'react-redux'
|
606 | *
|
607 | * export const ExampleComponent = () => {
|
608 | * const store = useStore()
|
609 | * return <div>{store.getState()}</div>
|
610 | * }
|
611 | */
|
612 | export function useStore<S = RootStateOrAny, A extends Action = AnyAction>(): Store<S, A>;
|
613 |
|
614 | /**
|
615 | * Hook factory, which creates a `useSelector` hook bound to a given context.
|
616 | *
|
617 | * @param Context passed to your `<Provider>`.
|
618 | * @returns A `useSelector` hook bound to the specified context.
|
619 | */
|
620 | export function createSelectorHook<S = RootStateOrAny, A extends Action = AnyAction>(
|
621 | context?: Context<ReactReduxContextValue<S, A>>,
|
622 | ): <Selected extends unknown>(
|
623 | selector: (state: S) => Selected,
|
624 | equalityFn?: (previous: Selected, next: Selected) => boolean,
|
625 | ) => Selected;
|
626 |
|
627 | /**
|
628 | * Hook factory, which creates a `useStore` hook bound to a given context.
|
629 | *
|
630 | * @param Context passed to your `<Provider>`.
|
631 | * @returns A `useStore` hook bound to the specified context.
|
632 | */
|
633 | export function createStoreHook<S = RootStateOrAny, A extends Action = AnyAction>(
|
634 | context?: Context<ReactReduxContextValue<S, A>>,
|
635 | ): () => Store<S, A>;
|
636 |
|
637 | /**
|
638 | * Hook factory, which creates a `useDispatch` hook bound to a given context.
|
639 | *
|
640 | * @param Context passed to your `<Provider>`.
|
641 | * @returns A `useDispatch` hook bound to the specified context.
|
642 | */
|
643 | export function createDispatchHook<S = RootStateOrAny, A extends Action = AnyAction>(
|
644 | context?: Context<ReactReduxContextValue<S, A>>,
|
645 | ): () => Dispatch<A>;
|
646 |
|
647 | // tslint:enable:no-unnecessary-generics
|
648 |
|
\ | No newline at end of file |