Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comment count doesn't update in Scream component without refreshing #11

Open
AndrewYinLi opened this issue Jan 29, 2020 · 3 comments
Open

Comments

@AndrewYinLi
Copy link

AndrewYinLi commented Jan 29, 2020

@hidjou

Comment count doesn't update in Scream component, only updates in ScreamDialog if you don't refresh the page. This is because in our state we only update commentCount in scream (accessed by ScreamDialog and not also screams (accessed by Scream).

This can be fixed in dataReducer.js by replacing the code for case SUBMIT_COMMENT: with the following:

case SUBMIT_COMMENT:
      let commentedOnIndex = state.screams.findIndex(
        scream => scream.screamId === action.payload.screamId
      );
      return {
        ...state,
        scream: {
          ...state.scream,
          comments: [action.payload, ...state.scream.comments],
          commentCount: state.scream.commentCount + 1
        },
        screams: state.screams.map((scream, screamsArrIndex) =>
          screamsArrIndex === commentedOnIndex
            ? { ...scream, commentCount: scream.commentCount + 1 }
            : scream
        )
      };
@vincentSuriani
Copy link

Hi Andrew, I am facing the same issue. This fix didn't work for me :( Still needs a refresh. My initial situation is different though I am not seeing the change in the dialog or the post, when you said you started by being able to see the change in the dialog but not post. I had changed all the code to functional components though, and not sure if in my life-cycle methods is where the issue is happening. It updates after hitting the like button and on setTimeout(window.location.replace(),2000) (But doing the replace a big no no). I have to see what is going on with further investigation. I will post my results and fix if found.

@vincentSuriani
Copy link

nevermind that works perfectly. I had a typo in it. Thanks Andrew. 👍

@VatsalNarwal
Copy link

Solution

Yep, that is very true, it does not updates without reloading. To fix that and you can use useEffect feature in react. useEffect keeps on listening changes and whenever there is an update with the database it fires off a particular code. Hope that would help...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants