Last active
June 8, 2018 06:20
-
-
Save tlaitinen/cbf2ab5225648200545c505421bfc1a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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