Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.