Created
October 11, 2019 14:21
-
-
Save kana-sama/981d701b6abd0201031c85090c848729 to your computer and use it in GitHub Desktop.
This file contains 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
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