Skip to content

Instantly share code, notes, and snippets.

@kana-sama
Created October 11, 2019 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kana-sama/981d701b6abd0201031c85090c848729 to your computer and use it in GitHub Desktop.
Save kana-sama/981d701b6abd0201031c85090c848729 to your computer and use it in GitHub Desktop.
class Users {
constructor(http) {
this.http = http;
this.cache = new Map();
}
get(id, next) {
if (this.cache.has(id)) {
next(this.cache.get(id));
}
return this.http.get(`/users/${id}`).then(user => {
this.cache.set(id, user);
next(user);
});
}
}
const fetchCurrentUser = (dispatch, getState, { users }) => {
dispatch(currentUserFetching);
users.get(id, user => {
dispatch(currentUserFetched(user));
});
}
function CurrentUser() {
const dispatch = useDispatch();
const currentUserID = useSelector(state => state.currentUserID);
useEffect(() => dispatch(fetchCurrentUser()), [currentUserID]);
return <User id={currentUserID} />;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment