-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (50 loc) · 1.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React, { PureComponent } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as userInfoActionsFromOtherFile from '~actions/userinfo';
import LocalStore from '~util/localStore';
import { CITYNAME } from '~config/localStoreKey';
import Header from '~components/Header';
import CurrentCity from '~components/CurrentCity';
import CityList from '~components/CityList';
class City extends PureComponent {
render() {
return (
<div>
<Header title="选择城市" />
<CurrentCity cityName={this.props.userinfo.cityName} />
<CityList changeFn={this.changeCity} />
</div>
)
};
changeCity = newCity => {
// 将新选择的城市设置为当前城市
if (newCity == null) {
return;
};
// 1.修改redux
const userinfo = this.props.userinfo;
const actions = this.props.userInfoActions;
userinfo.cityName = newCity;
actions.update(userinfo);
// 2.修改localStorage
LocalStore.setItem(CITYNAME, newCity);
// 3.跳转到首页
this.props.history.push('/');
};
};
// ==========================redux react 绑定==========================
const mapStateToProps = state => {
return {
userinfo: state.userinfo
};
};
const mapDispatchToProps = dispatch => {
return {
userInfoActions: bindActionCreators(userInfoActionsFromOtherFile, dispatch)
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(City);