Skip to content
This repository was archived by the owner on Dec 9, 2021. It is now read-only.

Commit f787def

Browse files
authored
Add react-router-redux history push example (#12)
1 parent 6b20d98 commit f787def

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/views/home/Home.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react';
22
import {connect} from 'react-redux';
3+
import {push} from 'react-router-redux';
34
import UserAction from '../../stores/user/UserAction';
45
import MetaAction from '../../stores/meta/MetaAction';
56
import IStore from '../../stores/IStore';
@@ -13,6 +14,7 @@ interface IStateToProps {
1314
readonly user: IUserReducerState;
1415
}
1516
interface IDispatchToProps {
17+
historyPush: (route: string) => void;
1618
loadUser: () => void;
1719
setMeta: (meta: IMetaReducerState) => void;
1820
}
@@ -21,12 +23,15 @@ const mapStateToProps = (state: IStore): IStateToProps => ({
2123
user: state.userReducer,
2224
});
2325
const mapDispatchToProps = (dispatch: Dispatch<IStore>): IDispatchToProps => ({
26+
historyPush: (route: string) => dispatch(push(route)),
2427
loadUser: () => dispatch(UserAction.loadUser()),
2528
setMeta: (meta: IMetaReducerState) => dispatch(MetaAction.setMeta(meta)),
2629
});
2730

2831
class Home extends React.Component<IStateToProps & IDispatchToProps & IProps, IState> {
2932

33+
private _onClickPushExampleHandler: (event: React.MouseEvent<HTMLButtonElement>) => void = this._onClickPushExample.bind(this);
34+
3035
public componentWillMount(): void {
3136
this.props.setMeta({
3237
title: 'Home Page',
@@ -55,10 +60,17 @@ class Home extends React.Component<IStateToProps & IDispatchToProps & IProps, IS
5560
</button>
5661
</p>
5762
</div>
63+
<button onClick={this._onClickPushExampleHandler}>{'Go to About'}</button>
5864
</div>
5965
);
6066
}
6167

68+
private _onClickPushExample(event: React.MouseEvent<HTMLButtonElement>): void {
69+
event.preventDefault();
70+
71+
this.props.historyPush('/About');
72+
}
73+
6274
}
6375

6476
export default connect<IStateToProps, IDispatchToProps, IProps>(mapStateToProps, mapDispatchToProps)(Home);

0 commit comments

Comments
 (0)