[react-redux] Add createProvider (#20052)

* Add ConnectFunction interface

* Add createProvider function declaration

* Add documentation for createProvider

* Update documentation for Connect

* Add new author

* Add test for createProvider
This commit is contained in:
Dibyo Majumdar
2017-10-02 13:10:11 -07:00
committed by Ryan Cavanaugh
parent 9764208729
commit 2717cfa52c
2 changed files with 127 additions and 66 deletions
+73 -56
View File
@@ -6,6 +6,7 @@
// Curits Layne <https://github.com/clayne11>
// Frank Tan <https://github.com/tansongyang>
// Nicholas Boll <https://github.com/nicholasboll>
// Dibyo Majumdar <https://github.com/mdibyo>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
@@ -66,73 +67,80 @@ export type InferableComponentEnhancer<TInjectedProps> =
* @param mergeProps
* @param options
*/
export declare function connect(): InferableComponentEnhancer<DispatchProp<any>>;
export interface Connect {
(): InferableComponentEnhancer<DispatchProp<any>>;
export declare function connect<TStateProps = {}, no_dispatch = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>
): InferableComponentEnhancerWithProps<TStateProps & DispatchProp<any>, TOwnProps>;
<TStateProps = {}, no_dispatch = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>
): InferableComponentEnhancerWithProps<TStateProps & DispatchProp<any>, TOwnProps>;
export declare function connect<no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>
): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;
<no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>
): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;
export declare function connect<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>
): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;
<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>
): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;
export declare function connect<TStateProps = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: null | undefined,
mergeProps: MergeProps<TStateProps, undefined, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
<TStateProps = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: null | undefined,
mergeProps: MergeProps<TStateProps, undefined, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
export declare function connect<no_state = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: MergeProps<undefined, TDispatchProps, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
<no_state = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: MergeProps<undefined, TDispatchProps, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
export declare function connect<no_state = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: null | undefined,
mergeProps: MergeProps<undefined, undefined, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
<no_state = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: null | undefined,
mergeProps: MergeProps<undefined, undefined, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
export declare function connect<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
export declare function connect<TStateProps = {}, no_dispatch = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: null | undefined,
mergeProps: null | undefined,
options: Options<TStateProps, TOwnProps>
): InferableComponentEnhancerWithProps<DispatchProp<any> & TStateProps, TOwnProps>;
<TStateProps = {}, no_dispatch = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: null | undefined,
mergeProps: null | undefined,
options: Options<TStateProps, TOwnProps>
): InferableComponentEnhancerWithProps<DispatchProp<any> & TStateProps, TOwnProps>;
export declare function connect<no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: null | undefined,
options: Options<no_state, TOwnProps>
): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;
<no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: null | undefined,
options: Options<no_state, TOwnProps>
): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;
export declare function connect<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: null | undefined,
options: Options<TStateProps, TOwnProps>
): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;
<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: null | undefined,
options: Options<TStateProps, TOwnProps>
): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;
export declare function connect<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
options: Options<TStateProps, TOwnProps, TMergedProps>
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
options: Options<TStateProps, TOwnProps, TMergedProps>
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
}
/**
* The connect function. See {@type Connect} for details.
*/
export declare const connect: Connect;
interface MapStateToProps<TStateProps, TOwnProps> {
(state: any, ownProps: TOwnProps): TStateProps;
@@ -284,3 +292,12 @@ export interface ProviderProps {
* Makes the Redux store available to the connect() calls in the component hierarchy below.
*/
export class Provider extends React.Component<ProviderProps, {}> { }
/**
* Creates a new <Provider> which will set the Redux Store on the passed key of the context. You probably only need this
* if you are in the inadvisable position of having multiple stores. You will also need to pass the same storeKey to the
* options argument of connect.
*
* @param storeKey The key of the context on which to set the store.
*/
export declare function createProvider(storeKey: string): typeof Provider;
+54 -10
View File
@@ -1,8 +1,8 @@
import { Component, ReactElement } from 'react';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Store, Dispatch, ActionCreator, bindActionCreators, ActionCreatorsMapObject } from 'redux';
import { connect, Provider, DispatchProp, MapStateToProps } from 'react-redux';
import { Store, Dispatch, ActionCreator, createStore, bindActionCreators, ActionCreatorsMapObject } from 'redux';
import { Connect, connect, createProvider, Provider, DispatchProp, MapStateToProps, Options } from 'react-redux';
import objectAssign = require('object-assign');
//
@@ -14,10 +14,10 @@ import objectAssign = require('object-assign');
// output of `connect` to make sure the signature is what is expected
namespace Empty {
interface OwnProps { foo: string, dispatch: Dispatch<any> }
interface OwnProps { foo: string, dispatch: Dispatch<any> }
class TestComponent extends Component<OwnProps> {}
const Test = connect()(TestComponent)
const verify = <Test foo='bar' />
@@ -90,7 +90,7 @@ namespace MapDispatch {
)(TestComponent)
const verifyNull = <TestNull foo='bar' />
const TestUndefined = connect(
undefined,
mapDispatchToProps,
@@ -140,7 +140,7 @@ namespace MapDispatchFactory {
)(TestComponent)
const verifyNull = <TestNull foo='bar' />
const TestUndefined = connect(
undefined,
mapDispatchToPropsFactory,
@@ -176,11 +176,11 @@ namespace MapStateFactoryAndDispatch {
interface OwnProps { foo: string }
interface StateProps { bar: number }
interface DispatchProps { onClick: () => void }
const mapStateToPropsFactory = () => () =>({
bar: 1
})
const mapDispatchToProps = () => ({
onClick: () => {}
})
@@ -199,11 +199,11 @@ namespace MapStateFactoryAndDispatchFactory {
interface OwnProps { foo: string }
interface StateProps { bar: number }
interface DispatchProps { onClick: () => void }
const mapStateToPropsFactory = () => () =>({
bar: 1
})
const mapDispatchToPropsFactory = () => () => ({
onClick: () => {}
})
@@ -850,3 +850,47 @@ namespace TestWrappedComponent {
// `Connected` does not require explicit `name` prop
const TestConnected = (props: any) => <Connected />;
}
namespace TestCreateProvider {
const STORE_KEY = 'myStore';
const MyStoreProvider = createProvider(STORE_KEY);
const myStoreConnect: Connect = function(
mapStateToProps?: any,
mapDispatchToProps?: any,
mergeProps?: any,
options: Options = {},
) {
options.storeKey = STORE_KEY;
return connect(
mapStateToProps,
mapDispatchToProps,
mergeProps,
options,
);
};
interface State { a: number };
const store = createStore<State>(() => ({ a: 1 }));
const myStore = createStore<State>(() => ({ a: 2 }));
interface AProps { a: number };
const A = (props: AProps) => (<h1>A is {props.a}</h1>);
const A1 = connect<AProps>(state => state)(A);
const A2 = myStoreConnect<AProps>(state => state)(A);
const Combined = () => (
<Provider store={store}>
<MyStoreProvider store={myStore}>
<A1 />
<A2 />
</MyStoreProvider>
</Provider>
);
// This renders:
// <h1>A is 1</h1>
// <h1>A is 2</h1>
ReactDOM.render(<Combined />, document.body);
}