react-pwa icon indicating copy to clipboard operation
react-pwa copied to clipboard

recoil actions in useEffect

Open SecretUnicorn opened this issue 1 year ago • 1 comments

function useTeams() {
  const [teamState, setTeamState] = useRecoilState(teamsAtom);

  function init(teams) {
    setTeamState(teams.map(t => ({
      ...t,
      answered: false,
      already_answered: false,
    })));
  }

  function teamAnswered(teamId) {
    setTeamState(teamState.map(t => ({
      ...t,
      answered: t.id === teamId,
    })));
  }

  return [teamState, { init, teamAnswered }];
useEffect(() => {
    if (lastMessage && lastMessage.data) {
          teamAnswered(data.data)
    }
  }, [lastMessage, teamAnswered]);

I want to change my recoil state in a useEffect. My linter tells me to add the "setTeam" function to the dep list of the useEffect. But when I use setTeam, team changes and setTeam does too which causes the useEffect to trigger endlessly. Is this intended or am I using the hook wrokng? Any help would be apreciated

SecretUnicorn avatar May 16 '23 20:05 SecretUnicorn