Skip to content

Instantly share code, notes, and snippets.

@tlaitinen
Last active June 8, 2018 06:20
Show Gist options
  • Select an option

  • Save tlaitinen/cbf2ab5225648200545c505421bfc1a5 to your computer and use it in GitHub Desktop.

Select an option

Save tlaitinen/cbf2ab5225648200545c505421bfc1a5 to your computer and use it in GitHub Desktop.
import {connect} from 'react-redux';
import {bindActionCreators, ActionCreatorsMapObject} from 'redux';
import {RootState, Dispatch} from '../reducers';
export interface PropsMapper<StateProps, DispatchProps, OwnProps> {
fromState: (state:RootState, ownProps:OwnProps) => StateProps;
actions: (ownProps:OwnProps) => DispatchProps;
}
export type PropsOf<T> = T extends PropsMapper<infer StateProps, infer DispatchProps, infer OwnProps>
? StateProps & DispatchProps & OwnProps
: never;
export function appConnect<
StateProps,
DispatchProps extends ActionCreatorsMapObject,
OwnProps>(mapper:PropsMapper<StateProps, DispatchProps, OwnProps>) {
return connect<StateProps, DispatchProps, OwnProps, RootState>(
mapper.fromState,
(dispatch:Dispatch, ownProps:OwnProps) => bindActionCreators(mapper.actions(ownProps), dispatch)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment