This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
MainPage.js
159 lines (149 loc) · 4.91 KB
/
MainPage.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { addUrlProps, UrlQueryParamTypes } from 'react-url-query';
import { changeArr, changeBaz, changeFoo, changeBar, changeMany } from './state/actions';
/**
* Specify how the URL gets decoded here. This is an object that takes the prop
* name as a key, and a query param specifier as the value. The query param
* specifier can have a `type`, indicating how to decode the value from the
* URL, and a `queryParam` field that indicates which key in the query
* parameters should be read (this defaults to the prop name if not provided).
*
* The queryParam value for `foo` here matches the one used in the changeFoo
* action.
*/
const urlPropsQueryConfig = {
arr: { type: UrlQueryParamTypes.array },
bar: { type: UrlQueryParamTypes.string },
foo: { type: UrlQueryParamTypes.number, queryParam: 'fooInUrl' },
}
/**
* Standard react-redux mapStateToProps -- maps state from the Redux store to
* the `baz` prop in MainPage. Used via the higher-order component `connect`.
*/
function mapStateToProps(state, props) {
return {
baz: state.baz,
};
}
/**
* Standard react-redux mapDispatchToProps
*/
function mapDispatchToProps(dispatch) {
return {
onChangeArr: (arr) => dispatch(changeArr(arr)),
onChangeFoo: (foo) => dispatch(changeFoo(foo)),
onChangeBar: (bar) => dispatch(changeBar(bar)),
onChangeBaz: (baz) => dispatch(changeBaz(baz)),
onChangeMany: (foo) => dispatch(changeMany({ foo })),
};
}
/**
* The MainPage container. Note that none of the code within this component
* indicates which values are stored in the URL and which are stored in the Redux
* store.
*/
class MainPage extends PureComponent {
static propTypes = {
arr: PropTypes.array,
bar: PropTypes.string,
baz: PropTypes.string,
foo: PropTypes.number,
onChangeArr: PropTypes.func,
onChangeBar: PropTypes.func,
onChangeBaz: PropTypes.func,
onChangeFoo: PropTypes.func,
onChangeMany: PropTypes.func,
}
static defaultProps = {
arr: [],
bar: 'bar',
baz: 'baz',
foo: 123,
}
/**
* Log whether or not we are getting a new array decoded for `arr` each time
* we receive props. Ideally, this only happens when `arr` changes.
*
* We are using urlPropsQueryConfig, so the decoding is handled by addUrlProps
* as opposed to mapUrlToProps. The decoding within addUrlProps only re-decodes
* a value if it has changed, so we get the desired behavior.
*/
componentWillReceiveProps(nextProps) {
const { arr } = this.props;
if (arr !== nextProps.arr) {
console.log('got new arr:', arr, '->', nextProps.arr);
} else {
console.log('arr did not change:', arr, '===', nextProps.arr);
}
}
render() {
const { arr, foo, bar, baz, onChangeArr, onChangeBar, onChangeBaz,
onChangeFoo, onChangeMany } = this.props;
return (
<div>
<table>
<tbody>
<tr>
<td>arr</td>
<td>{JSON.stringify(arr)}</td>
<td>(url query param)</td>
<td>
<button onClick={() => onChangeArr([Math.round(Math.random() * 9), Math.round(Math.random() * 9)])}>
Change arr
</button>
</td>
</tr>
<tr>
<td>foo</td>
<td>{foo}</td>
<td>(url query param)</td>
<td>
<button onClick={() => onChangeFoo(Math.round(Math.random() * 1000))}>
Change foo
</button>
</td>
</tr>
<tr>
<td>bar</td>
<td>{bar}</td>
<td>(url query param)</td>
<td>
<button onClick={() => onChangeBar(Math.random().toString(32).substring(8))}>
Change bar
</button>
</td>
</tr>
<tr>
<td>baz</td>
<td>{baz}</td>
<td>(redux state)</td>
<td>
<button onClick={() => onChangeBaz(Math.random().toString(32).substring(10))}>
Change baz
</button>
</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>
<button onClick={() => onChangeMany(Math.round(Math.random() * 2000))}>
Change many
</button>
</td>
</tr>
</tbody>
</table>
</div>
);
}
}
/**
* We use the addUrlProps higher-order component to map URL query parameters
* to props for MainPage. In this case the mapping happens automatically by
* first decoding the URL query parameters based on the urlPropsQueryConfig.
*/
export default addUrlProps({ urlPropsQueryConfig })(connect(mapStateToProps, mapDispatchToProps)(MainPage));