Last active
March 6, 2017 19:57
-
-
Save bultas/26f6d7a10eca23288e197ae7db7bae53 to your computer and use it in GitHub Desktop.
fetching data in React - DataFetcher concept
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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