Skip to content

Instantly share code, notes, and snippets.

@bultas
Last active March 6, 2017 19:57
Show Gist options
  • Save bultas/26f6d7a10eca23288e197ae7db7bae53 to your computer and use it in GitHub Desktop.
Save bultas/26f6d7a10eca23288e197ae7db7bae53 to your computer and use it in GitHub Desktop.
fetching data in React - DataFetcher concept
BucketActions.createBucket(
initValue
);
// > bucketID
BucketActions.addToBucket(
bucketID,
value
);
// > true
BucketActions.removeBucket(
bucketID
);
// > true
const getters = {
propName: () => {},
propName2: () => {}
};
function prepareSelectors(bucketID) {
return {
propName: (state) => state.buckets[bucketID].propName,
propName2: (state) => state.buckets[bucketID].propName2
}
}
class Component extends React.Component {
constructor() {
this.state = {
allDataFetched: false
};
}
componentDidMount() {
fetchData(getters).then((data) => {
dispach(
BucketActions
.createBucket(data)
.then((bucketID) => {
this.setState({
bucketID,
allDataFetched: true
});
})
);
});
};
componentWillReceiveProps(nextProps) {
const wantNewData = shallowCompare(this.props, nextProps);
if (wantNewData) {
this.setState({
allDataFetched: false
}, () => {
fetchData(getters).then((data) => {
dispach(
BucketActions
.addToBucket(data)
.then(() => {
this.setState({
allDataFetched: true
});
})
);
});
});
}
}
componentWillUnmount() {
dispatch(
BucketActions.removeBucket(
this.state.bucketID
)
);
}
render() {
<Connector
selectors={prepareSelectors(this.state.bucketID)}
/>
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment