Skip to content

Instantly share code, notes, and snippets.

@productioncoder
Last active November 16, 2018 18:44
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 productioncoder/d8e236c1c30a8d5a40175e329aa572e1 to your computer and use it in GitHub Desktop.
Save productioncoder/d8e236c1c30a8d5a40175e329aa572e1 to your computer and use it in GitHub Desktop.
Youtube in React: incorporating the commentThread response into our global state
function reduceCommentThread(response, videoId, prevState) {
if (!response) {
return prevState;
}
const newComments = response.items.reduce((acc, item) => {
acc[item.id] = item;
return acc;
}, {});
// if we have already fetched some comments for a particular video
// we just append the ids for the new comments
const prevCommentIds = prevState.byVideo[videoId] ? prevState.byVideo[videoId].ids : [];
const commentIds = [...prevCommentIds, ...Object.keys(newComments)];
const byVideoComment = {
nextPageToken: response.nextPageToken,
ids: commentIds,
};
return {
...prevState,
byId: {
...prevState.byId,
...newComments,
},
byVideo: {
...prevState.byVideo,
[videoId]: byVideoComment,
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment