-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwithRemoteDataSource.js
More file actions
37 lines (30 loc) · 1018 Bytes
/
withRemoteDataSource.js
File metadata and controls
37 lines (30 loc) · 1018 Bytes
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
import React, { PropTypes } from 'react';
import { ListView } from 'react-native';
import _ from 'lodash/fp';
/**
* Adds remote data source logic to the Component
*
* @param selector {function} - selects source data from props
* @param Component {component} - the lower order component
*
* @return {component} - a component wrapped in HOC
*/
const withRemoteDataSource = (selector, Component) => class extends React.Component {
static propTypes = {
remoteDataSourceFetch: PropTypes.func.isRequired,
};
constructor(props) {
super(props);
this.state = { dataSource: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 }) };
}
componentWillReceiveProps(nextProps) {
this.setState({ dataSource: this.state.dataSource.cloneWithRows(selector(nextProps)) });
}
componentDidMount() {
this.props.remoteDataSourceFetch();
}
render() {
return <Component dataSource={this.state.dataSource} {...this.props} />;
}
};
export default _.curry(withRemoteDataSource);