UNPKG

28.9 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// Curits Layne <https://github.com/clayne11>
5// Frank Tan <https://github.com/tansongyang>
6// Nicholas Boll <https://github.com/nicholasboll>
7// Dibyo Majumdar <https://github.com/mdibyo>
8// Valentin Descamps <https://github.com/val1984>
9// Johann Rakotoharisoa <https://github.com/jrakotoharisoa>
10// Anatoli Papirovski <https://github.com/apapirovski>
11// Boris Sergeyev <https://github.com/surgeboris>
12// Søren Bruus Frank <https://github.com/soerenbf>
13// Jonathan Ziller <https://github.com/mrwolfz>
14// Dylan Vann <https://github.com/dylanvann>
15// Yuki Ito <https://github.com/Lazyuki>
16// Kazuma Ebina <https://github.com/kazuma1989>
17// Michael Lebedev <https://github.com/megazazik>
18// jun-sheaf <https://github.com/jun-sheaf>
19// Lenz Weber <https://github.com/phryneas>
20// Mark Erikson <https://github.com/markerikson>
21// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
22// TypeScript Version: 3.0
23
24import {
25 ClassAttributes,
26 Component,
27 ComponentClass,
28 Context,
29 JSXElementConstructor,
30 NamedExoticComponent,
31 ReactNode
32} from 'react';
33
34import {
35 Action,
36 AnyAction,
37 Dispatch,
38 Store
39} from 'redux';
40
41import hoistNonReactStatics = require('hoist-non-react-statics');
42
43/**
44 * This interface can be augmented by users to add default types for the root state when
45 * using `react-redux`.
46 * Use module augmentation to append your own type definition in a your_custom_type.d.ts file.
47 * https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
48 */
49// tslint:disable-next-line:no-empty-interface
50export interface DefaultRootState {}
51
52export type AnyIfEmpty<T extends object> = keyof T extends never ? any : T;
53export type RootStateOrAny = AnyIfEmpty<DefaultRootState>;
54
55// Omit taken from https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html
56export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
57
58export type DistributiveOmit<T, K extends keyof T> = T extends unknown ? Omit<T, K> : never;
59
60export interface DispatchProp<A extends Action = AnyAction> {
61 dispatch: Dispatch<A>;
62}
63
64export type AdvancedComponentDecorator<TProps, TOwnProps> =
65 (component: JSXElementConstructor<TProps>) => NamedExoticComponent<TOwnProps>;
66
67/**
68 * A property P will be present if:
69 * - it is present in DecorationTargetProps
70 *
71 * Its value will be dependent on the following conditions
72 * - if property P is present in InjectedProps and its definition extends the definition
73 * in DecorationTargetProps, then its definition will be that of DecorationTargetProps[P]
74 * - if property P is not present in InjectedProps then its definition will be that of
75 * DecorationTargetProps[P]
76 * - if property P is present in InjectedProps but does not extend the
77 * DecorationTargetProps[P] definition, its definition will be that of InjectedProps[P]
78 */
79export 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 * a property P will be present if :
89 * - it is present in both DecorationTargetProps and InjectedProps
90 * - InjectedProps[P] can satisfy DecorationTargetProps[P]
91 * ie: decorated component can accept more types than decorator is injecting
92 *
93 * For decoration, inject props or ownProps are all optionally
94 * required by the decorated (right hand side) component.
95 * But any property required by the decorated component must be satisfied by the injected property.
96 */
97export 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// Infers prop type from component C
105export type GetProps<C> = C extends JSXElementConstructor<infer P>
106 ? C extends ComponentClass<P> ? ClassAttributes<InstanceType<C>> & P : P
107 : never;
108
109// Applies LibraryManagedAttributes (proper handling of defaultProps
110// and propTypes).
111export type GetLibraryManagedProps<C> = JSX.LibraryManagedAttributes<C, GetProps<C>>;
112
113// Defines WrappedComponent and derives non-react statics.
114export type ConnectedComponent<
115 C extends JSXElementConstructor<any>,
116 P
117> = NamedExoticComponent<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 JSXElementConstructor<Matching<TInjectedProps, GetProps<C>>>>(
127 component: C
128 ) => ConnectedComponent<C, DistributiveOmit<GetLibraryManagedProps<C>, keyof Shared<TInjectedProps, GetLibraryManagedProps<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 /* eslint-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, 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 /* eslint-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 | undefined;
352
353 /**
354 * When pure, compares incoming store state to its previous value.
355 * @default strictEqual
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 */
395export function connectAdvanced<S, TProps, TOwnProps, TFactoryOptions = {}>(
396 // eslint-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) | 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
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> | undefined;
483 children?: ReactNode;
484}
485
486/**
487 * Makes the Redux store available to the connect() calls in the component hierarchy below.
488 */
489export 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 */
495export 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 */
501export 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 */
510export 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)
540export function useDispatch<TDispatch = Dispatch<any>>(): TDispatch;
541export 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 */
571export 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 */
588export 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 */
610export 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 */
618export 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 */
631export 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 */
641export 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