UNPKG

28.7 kBTypeScriptView Raw
1// Type definitions for react-redux 7.1
2// Project: https://github.com/reduxjs/react-redux
3// Definitions by: Qubo <https://github.com/tkqubo>,
4// Kenzie Togami <https://github.com/kenzierocks>,
5// Curits Layne <https://github.com/clayne11>
6// Frank Tan <https://github.com/tansongyang>
7// Nicholas Boll <https://github.com/nicholasboll>
8// Dibyo Majumdar <https://github.com/mdibyo>
9// Thomas Charlat <https://github.com/kallikrein>
10// Valentin Descamps <https://github.com/val1984>
11// Johann Rakotoharisoa <https://github.com/jrakotoharisoa>
12// Anatoli Papirovski <https://github.com/apapirovski>
13// Boris Sergeyev <https://github.com/surgeboris>
14// Søren Bruus Frank <https://github.com/soerenbf>
15// Jonathan Ziller <https://github.com/mrwolfz>
16// Dylan Vann <https://github.com/dylanvann>
17// Yuki Ito <https://github.com/Lazyuki>
18// Kazuma Ebina <https://github.com/kazuma1989>
19// Michael Lebedev <https://github.com/megazazik>
20// jun-sheaf <https://github.com/jun-sheaf>
21// Lenz Weber <https://github.com/phryneas>
22// Mark Erikson <https://github.com/markerikson>
23// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
24// TypeScript Version: 3.0
25
26import {
27 ClassAttributes,
28 Component,
29 ComponentClass,
30 ComponentType,
31 StatelessComponent,
32 Context,
33 NamedExoticComponent
34} from 'react';
35
36import {
37 Action,
38 ActionCreator,
39 AnyAction,
40 Dispatch,
41 Store
42} from 'redux';
43
44import hoistNonReactStatics = require('hoist-non-react-statics');
45
46/**
47 * This interface can be augmented by users to add default types for the root state when
48 * using `react-redux`.
49 * Use module augmentation to append your own type definition in a your_custom_type.d.ts file.
50 * https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
51 */
52// tslint:disable-next-line:no-empty-interface
53export interface DefaultRootState {}
54
55export type AnyIfEmpty<T extends object> = keyof T extends never ? any : T;
56export type RootStateOrAny = AnyIfEmpty<DefaultRootState>;
57
58// Omit taken from https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html
59export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
60
61export type DistributiveOmit<T, K extends keyof T> = T extends unknown ? Omit<T, K> : never;
62
63export interface DispatchProp<A extends Action = AnyAction> {
64 dispatch: Dispatch<A>;
65}
66
67export type AdvancedComponentDecorator<TProps, TOwnProps> =
68 (component: ComponentType<TProps>) => NamedExoticComponent<TOwnProps>;
69
70/**
71 * A property P will be present if:
72 * - it is present in DecorationTargetProps
73 *
74 * Its value will be dependent on the following conditions
75 * - if property P is present in InjectedProps and its definition extends the definition
76 * in DecorationTargetProps, then its definition will be that of DecorationTargetProps[P]
77 * - if property P is not present in InjectedProps then its definition will be that of
78 * DecorationTargetProps[P]
79 * - if property P is present in InjectedProps but does not extend the
80 * DecorationTargetProps[P] definition, its definition will be that of InjectedProps[P]
81 */
82export 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 * a property P will be present if :
92 * - it is present in both DecorationTargetProps and InjectedProps
93 * - InjectedProps[P] can satisfy DecorationTargetProps[P]
94 * ie: decorated component can accept more types than decorator is injecting
95 *
96 * For decoration, inject props or ownProps are all optionally
97 * required by the decorated (right hand side) component.
98 * But any property required by the decorated component must be satisfied by the injected property.
99 */
100export 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// Infers prop type from component C
108export type GetProps<C> = C extends ComponentType<infer P>
109 ? C extends ComponentClass<P> ? ClassAttributes<InstanceType<C>> & P : P
110 : never;
111
112// Applies LibraryManagedAttributes (proper handling of defaultProps
113// and propTypes), as well as defines WrappedComponent.
114export type ConnectedComponent<
115 C extends ComponentType<any>,
116 P
117> = NamedExoticComponent<JSX.LibraryManagedAttributes<C, P>> & hoistNonReactStatics.NonReactStatics<C> & {
118 WrappedComponent: C;
119};
120
121// Injects props and removes them from the prop requirements.
122// Will not pass through the injected props if they are passed in during
123// render. Also adds new prop requirements from TNeedsProps.
124// Uses distributive omit to preserve discriminated unions part of original prop type
125export type InferableComponentEnhancerWithProps<TInjectedProps, TNeedsProps> =
126 <C extends ComponentType<Matching<TInjectedProps, GetProps<C>>>>(
127 component: C
128 ) => ConnectedComponent<C, DistributiveOmit<GetProps<C>, keyof Shared<TInjectedProps, GetProps<C>>> & TNeedsProps>;
129
130// Injects props and removes them from the prop requirements.
131// Will not pass through the injected props if they are passed in during
132// render.
133export type InferableComponentEnhancer<TInjectedProps> =
134 InferableComponentEnhancerWithProps<TInjectedProps, {}>;
135
136export type InferThunkActionCreatorType<TActionCreator extends (...args: any[]) => any> =
137 TActionCreator extends (...args: infer TParams) => (...args: any[]) => infer TReturn
138 ? (...args: TParams) => TReturn
139 : TActionCreator;
140
141export type HandleThunkActionCreator<TActionCreator> =
142 TActionCreator extends (...args: any[]) => any
143 ? InferThunkActionCreatorType<TActionCreator>
144 : TActionCreator;
145
146// redux-thunk middleware returns thunk's return value from dispatch call
147// https://github.com/reduxjs/redux-thunk#composition
148export type ResolveThunks<TDispatchProps> =
149 TDispatchProps extends { [key: string]: any }
150 ? {
151 [C in keyof TDispatchProps]: HandleThunkActionCreator<TDispatchProps[C]>
152 }
153 : TDispatchProps;
154
155// the conditional type is to support TypeScript 3.0, which does not support mapping over tuples and arrays;
156// once the typings are updated to at least TypeScript 3.1, a simple mapped type can replace this mess
157export 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 * Connects a React component to a Redux store.
182 *
183 * - Without arguments, just wraps the component, without changing the behavior / props
184 *
185 * - If 2 params are passed (3rd param, mergeProps, is skipped), default behavior
186 * is to override ownProps (as stated in the docs), so what remains is everything that's
187 * not a state or dispatch prop
188 *
189 * - When 3rd param is passed, we don't know if ownProps propagate and whether they
190 * should be valid component props, because it depends on mergeProps implementation.
191 * As such, it is the user's responsibility to extend ownProps interface from state or
192 * dispatch props or both when applicable
193 *
194 * @param mapStateToProps
195 * @param mapDispatchToProps
196 * @param mergeProps
197 * @param options
198 */
199export interface Connect<DefaultState = DefaultRootState> {
200 // tslint:disable:no-unnecessary-generics
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, undefined, 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, undefined, 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 // tslint:enable:no-unnecessary-generics
299}
300
301/**
302 * Infers the type of props that a connector will inject into a component.
303 */
304export 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 * The connect function. See {@type Connect} for details.
315 */
316export const connect: Connect;
317
318export type MapStateToProps<TStateProps, TOwnProps, State = DefaultRootState> =
319 (state: State, ownProps: TOwnProps) => TStateProps;
320
321export type MapStateToPropsFactory<TStateProps, TOwnProps, State = DefaultRootState> =
322 (initialState: State, ownProps: TOwnProps) => MapStateToProps<TStateProps, TOwnProps, State>;
323
324export type MapStateToPropsParam<TStateProps, TOwnProps, State = DefaultRootState> =
325 MapStateToPropsFactory<TStateProps, TOwnProps, State> | MapStateToProps<TStateProps, TOwnProps, State> | null | undefined;
326
327export type MapDispatchToPropsFunction<TDispatchProps, TOwnProps> =
328 (dispatch: Dispatch<Action>, ownProps: TOwnProps) => TDispatchProps;
329
330export type MapDispatchToProps<TDispatchProps, TOwnProps> =
331 MapDispatchToPropsFunction<TDispatchProps, TOwnProps> | TDispatchProps;
332
333export type MapDispatchToPropsFactory<TDispatchProps, TOwnProps> =
334 (dispatch: Dispatch<Action>, ownProps: TOwnProps) => MapDispatchToPropsFunction<TDispatchProps, TOwnProps>;
335
336export type MapDispatchToPropsParam<TDispatchProps, TOwnProps> = MapDispatchToPropsFactory<TDispatchProps, TOwnProps> | MapDispatchToProps<TDispatchProps, TOwnProps>;
337
338export type MapDispatchToPropsNonObject<TDispatchProps, TOwnProps> = MapDispatchToPropsFactory<TDispatchProps, TOwnProps> | MapDispatchToPropsFunction<TDispatchProps, TOwnProps>;
339
340export type MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps> =
341 (stateProps: TStateProps, dispatchProps: TDispatchProps, ownProps: TOwnProps) => TMergedProps;
342
343export interface Options<State = DefaultRootState, TStateProps = {}, TOwnProps = {}, TMergedProps = {}> extends ConnectOptions {
344 /**
345 * If true, implements shouldComponentUpdate and shallowly compares the result of mergeProps,
346 * preventing unnecessary updates, assuming that the component is a “pure” component
347 * and does not rely on any input or state other than its props and the selected Redux store’s state.
348 * Defaults to true.
349 * @default true
350 */
351 pure?: boolean;
352
353 /**
354 * When pure, compares incoming store state to its previous value.
355 * @default strictEqual
356 */
357 areStatesEqual?: (nextState: State, prevState: State) => boolean;
358
359 /**
360 * When pure, compares incoming props to its previous value.
361 * @default shallowEqual
362 */
363 areOwnPropsEqual?: (nextOwnProps: TOwnProps, prevOwnProps: TOwnProps) => boolean;
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;
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;
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;
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 */
395export function connectAdvanced<S, TProps, TOwnProps, TFactoryOptions = {}>(
396 // tslint:disable-next-line no-unnecessary-generics
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 */
409export type SelectorFactory<S, TProps, TOwnProps, TFactoryOptions> =
410 (dispatch: Dispatch<Action>, factoryOptions: TFactoryOptions) => Selector<S, TProps, TOwnProps>;
411
412export type Selector<S, TProps, TOwnProps = null> = TOwnProps extends null | undefined
413 ? (state: S) => TProps
414 : (state: S, ownProps: TOwnProps) => TProps;
415
416export interface ConnectOptions {
417 /**
418 * Computes the connector component's displayName property relative to that of the wrapped component. Usually
419 * overridden by wrapper functions.
420 *
421 * @default name => 'ConnectAdvanced('+name+')'
422 * @param componentName
423 */
424 getDisplayName?: (componentName: string) => string;
425 /**
426 * Shown in error messages. Usually overridden by wrapper functions.
427 *
428 * @default 'connectAdvanced'
429 */
430 methodName?: string;
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;
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;
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;
453 /**
454 * @deprecated Use forwardRef
455 *
456 * @default false
457 */
458 withRef?: boolean;
459 /**
460 * The react context to get the store from.
461 *
462 * @default ReactReduxContext
463 */
464 context?: Context<ReactReduxContextValue>;
465}
466
467export interface ReactReduxContextValue<SS = any, A extends Action = AnyAction> {
468 store: Store<SS, A>;
469 storeState: SS;
470}
471
472export 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>;
483}
484
485/**
486 * Makes the Redux store available to the connect() calls in the component hierarchy below.
487 */
488export class Provider<A extends Action = AnyAction> extends Component<ProviderProps<A>> { }
489
490/**
491 * Exposes the internal context used in react-redux. It is generally advised to use the connect HOC to connect to the
492 * redux store instead of this approach.
493 */
494export const ReactReduxContext: Context<ReactReduxContextValue>;
495
496/**
497 * Wraps ReactDOM or React Native's internal unstable_batchedUpdate function. You can use it to ensure that
498 * multiple actions dispatched outside of React only result in a single render update.
499 */
500export function batch(cb: () => void): void;
501
502// tslint:disable:no-unnecessary-generics
503
504/**
505 * Compares two arbitrary values for shallow equality. Object values are compared based on their keys, i.e. they must
506 * have the same keys and for each key the value must be equal according to the `Object.is()` algorithm. Non-object
507 * values are also compared with the same algorithm as `Object.is()`.
508 */
509export function shallowEqual<T>(left: T, right: any): boolean;
510
511/**
512 * A hook to access the redux `dispatch` function.
513 *
514 * Note for `redux-thunk` users: the return type of the returned `dispatch` functions for thunks is incorrect.
515 * However, it is possible to get a correctly typed `dispatch` function by creating your own custom hook typed
516 * from the store's dispatch function like this: `const useThunkDispatch = () => useDispatch<typeof store.dispatch>();`
517 *
518 * @returns redux store's `dispatch` function
519 *
520 * @example
521 *
522 * import React from 'react'
523 * import { useDispatch } from 'react-redux'
524 *
525 * export const CounterComponent = ({ value }) => {
526 * const dispatch = useDispatch()
527 * return (
528 * <div>
529 * <span>{value}</span>
530 * <button onClick={() => dispatch({ type: 'increase-counter' })}>
531 * Increase counter
532 * </button>
533 * </div>
534 * )
535 * }
536 */
537// NOTE: the first overload below and note above can be removed if redux-thunk typings add an overload for
538// the Dispatch function (see also this PR: https://github.com/reduxjs/redux-thunk/pull/247)
539export function useDispatch<TDispatch = Dispatch<any>>(): TDispatch;
540export function useDispatch<A extends Action = AnyAction>(): Dispatch<A>;
541
542/**
543 * A hook to access the redux store's state. This hook takes a selector function
544 * as an argument. The selector is called with the store state.
545 *
546 * This hook takes an optional equality comparison function as the second parameter
547 * that allows you to customize the way the selected state is compared to determine
548 * whether the component needs to be re-rendered.
549 *
550 * If you do not want to have to specify the root state type for whenever you use
551 * this hook with an inline selector you can use the `TypedUseSelectorHook` interface
552 * to create a version of this hook that is properly typed for your root state.
553 *
554 * @param selector the selector function
555 * @param equalityFn the function that will be used to determine equality
556 *
557 * @returns the selected state
558 *
559 * @example
560 *
561 * import React from 'react'
562 * import { useSelector } from 'react-redux'
563 * import { RootState } from './store'
564 *
565 * export const CounterComponent = () => {
566 * const counter = useSelector((state: RootState) => state.counter)
567 * return <div>{counter}</div>
568 * }
569 */
570export function useSelector<TState = DefaultRootState, TSelected = unknown>(
571 selector: (state: TState) => TSelected,
572 equalityFn?: (left: TSelected, right: TSelected) => boolean
573): TSelected;
574
575/**
576 * This interface allows you to easily create a hook that is properly typed for your
577 * store's root state.
578 *
579 * @example
580 *
581 * interface RootState {
582 * property: string;
583 * }
584 *
585 * const useTypedSelector: TypedUseSelectorHook<RootState> = useSelector;
586 */
587export interface TypedUseSelectorHook<TState> {
588 <TSelected>(
589 selector: (state: TState) => TSelected,
590 equalityFn?: (left: TSelected, right: TSelected) => boolean
591 ): TSelected;
592}
593
594/**
595 * A hook to access the redux store.
596 *
597 * @returns the redux store
598 *
599 * @example
600 *
601 * import React from 'react'
602 * import { useStore } from 'react-redux'
603 *
604 * export const ExampleComponent = () => {
605 * const store = useStore()
606 * return <div>{store.getState()}</div>
607 * }
608 */
609export function useStore<S = RootStateOrAny, A extends Action = AnyAction>(): Store<S, A>;
610
611/**
612 * Hook factory, which creates a `useSelector` hook bound to a given context.
613 *
614 * @param Context passed to your `<Provider>`.
615 * @returns A `useSelector` hook bound to the specified context.
616 */
617export function createSelectorHook<S = RootStateOrAny, A extends Action = AnyAction>(
618 context?: Context<ReactReduxContextValue<S, A>>,
619): <Selected extends unknown>(
620 selector: (state: S) => Selected,
621 equalityFn?: (previous: Selected, next: Selected) => boolean,
622) => Selected;
623
624/**
625 * Hook factory, which creates a `useStore` hook bound to a given context.
626 *
627 * @param Context passed to your `<Provider>`.
628 * @returns A `useStore` hook bound to the specified context.
629 */
630export function createStoreHook<S = RootStateOrAny, A extends Action = AnyAction>(
631 context?: Context<ReactReduxContextValue<S, A>>,
632): () => Store<S, A>;
633
634/**
635 * Hook factory, which creates a `useDispatch` hook bound to a given context.
636 *
637 * @param Context passed to your `<Provider>`.
638 * @returns A `useDispatch` hook bound to the specified context.
639 */
640export function createDispatchHook<S = RootStateOrAny, A extends Action = AnyAction>(
641 context?: Context<ReactReduxContextValue<S, A>>,
642): () => Dispatch<A>;
643
644// tslint:enable:no-unnecessary-generics