Skip to content

Instantly share code, notes, and snippets.

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;
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 {
byId: {
byVideo: {
[videoId]: byVideoComment,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment