UNPKG

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