Merge pull request #19143 from coreh/fix/react-redux-dispatch-to-props-object

react-redux: fix dispatchToProps being passed as object
This commit is contained in:
Bowden Kelly
2017-08-22 10:52:37 -07:00
committed by GitHub
2 changed files with 24 additions and 8 deletions

View File

@@ -148,12 +148,8 @@ interface MapDispatchToPropsFunction<TDispatchProps, TOwnProps> {
(dispatch: Dispatch<any>, ownProps: TOwnProps): TDispatchProps;
}
interface MapDispatchToPropsObject {
[name: string]: ActionCreator<any>;
}
type MapDispatchToProps<TDispatchProps, TOwnProps> =
MapDispatchToPropsFunction<TDispatchProps, TOwnProps> | MapDispatchToPropsObject;
MapDispatchToPropsFunction<TDispatchProps, TOwnProps> | TDispatchProps;
interface MapDispatchToPropsFactory<TDispatchProps, TOwnProps> {
(dispatch: Dispatch<any>, ownProps: TOwnProps): MapDispatchToProps<TDispatchProps, TOwnProps>;
@@ -180,13 +176,13 @@ interface Options<TStateProps = {}, TOwnProps = {}, TMergedProps = {}> extends C
* @default strictEqual
*/
areStatesEqual?: (nextState: any, prevState: any) => boolean;
/**
* When pure, compares incoming props to its previous value.
* @default shallowEqual
*/
areOwnPropsEqual?: (nextOwnProps: TOwnProps, prevOwnProps: TOwnProps) => boolean;
/**
* When pure, compares the result of mapStateToProps to its previous value.
* @default shallowEqual

View File

@@ -1,7 +1,7 @@
import { Component, ReactElement } from 'react';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Store, Dispatch, bindActionCreators } from 'redux';
import { Store, Dispatch, ActionCreator, bindActionCreators } from 'redux';
import { connect, Provider, DispatchProp, MapStateToProps } from 'react-redux';
import objectAssign = require('object-assign');
@@ -549,3 +549,23 @@ namespace TestControlledComponentWithoutDispatchProp {
const MyControlledComponent = connect(mapStateToProps)(MyComponent);
const MyControlledFuncComponent = connect(mapStateToProps)(MyFuncComponent);
}
namespace TestDispatchToPropsAsObject {
const onClick: ActionCreator<{}> = null;
const mapStateToProps = (state: any) => {
return {
title: state.app.title as string,
};
};
const dispatchToProps = {
onClick,
};
type Props = { title: string; } & typeof dispatchToProps;
const HeaderComponent: React.StatelessComponent<Props> = (props) => {
return <h1>{props.title}</h1>;
}
const Header = connect(mapStateToProps, dispatchToProps)(HeaderComponent);
<Header />
}