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