Created
September 12, 2017 21:00
-
-
Save mikebridge/acde0aab22b9910601bb7bff613edbfb to your computer and use it in GitHub Desktop.
Sharing Typescript example....
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 * as React from "react"; | |
import * as ReactRedux from "react-redux"; | |
interface ICity { | |
cityName: string; | |
img: string; | |
} | |
// Action Creator | |
export const getCityInformation = () => ({ | |
type: "GET_ALL_CITIES" | |
}); | |
// HomePage Component | |
interface IHomePageOwnProps { | |
// this is empty so that react-redux can extract an empty type | |
} | |
interface IHomePageStoreProps { | |
allCityInformation: ICity[]; | |
} | |
interface IHomePageDispatchProps { | |
getAllCityInformation: () => void; | |
} | |
type IHomePageProps = | |
IHomePageOwnProps & | |
IHomePageStoreProps & | |
IHomePageDispatchProps; | |
class HomePage extends React.Component<IHomePageProps> { | |
componentDidMount() { | |
this.props.getAllCityInformation(); | |
} | |
render() { | |
return ( | |
<div> | |
... | |
</div> | |
); | |
} | |
} | |
const mapStateToProps = (state: any, ownProps: IHomePageOwnProps): IHomePageStoreProps => ({ | |
allCityInformation: state.cities // or wherever this is in your redux store | |
}); | |
// This works | |
// const mapDispatchToProps = (dispatch: any, ownProps: IHomePageOwnProps): IHomePageDispatchProps => ({ | |
// getAllCityInformation: dispatch(getCityInformation()) | |
// }); | |
// This doesn't | |
const mapDispatchToProps = { | |
getCityInformation | |
}; | |
export default ReactRedux.connect(mapStateToProps, mapDispatchToProps)(HomePage); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gives me this error