/
ReduxView.jsx
49 lines (39 loc) · 1.15 KB
/
ReduxView.jsx
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
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import isEqual from 'lodash/isEqual';
export default class extends Component {
constructor(props, context) {
super(props, context);
this.store = context.store;
this.connect = this.connect || connect;
}
static contextTypes = {
store: PropTypes.object
}
component = <div />;
initialize = () => {};
terminate = () => {};
componentWillMount = () => {
this.initialize(this.store, this.props);
}
componentWillReceiveProps = ({ params, location }) => {
// If params or location have changed we are on a new page.
if (!isEqual(params, this.props.params) || location !== this.props.location) {
this.terminate(this.store, this.props);
this.initialize(this.store, this.props);
}
}
componentWillUnmount = () => {
this.terminate(this.store, this.props);
}
render = () => {
const Component = this.connect(
this.mapStateToProps,
this.mapDispatchToProps,
this.mergeProps,
this.options
)(this.component);
return <Component { ...this.props }></Component>;
}
}