question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

a little confused about setproject hooks code

See original GitHub issue

Hi

i got little confused about the follow code

export const useProjects = () => {
  const [projects, setProjects] = useState([]);

  useEffect(() => {
    firebase
      .firestore()
      .collection('projects')
      .where('userId', '==', 'jlIFXIwyAL3tzHMtzRbw')
      .orderBy('projectId')
      .get()
      .then(snapshot => {
        const allProjects = snapshot.docs.map(project => ({
          ...project.data(),
          docId: project.id,
        }));

        if (JSON.stringify(allProjects) !== JSON.stringify(projects)) {
          setProjects(allProjects);
        }
      });
  }, [projects]);

  return { projects, setProjects };
};

i thought, if component calls setProject, the hooks will update projects, and the UI will got updated. after that, the effect will got fired, which will update the UI again.

should that reading from firebase just get called in app mounting time, and when component calls setProject, we just need to save it to firebase.

sorry for the poor english, the about maybe seems a little rude, but i am not meaning it

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vp5hcommented, Sep 18, 2021
const allProjects = snapshot.docs.map((project) => ({
          name: project.data().name,
          userId: project.data().userId,
          projectId: project.data().projectId,
          docId: project.id,
        }));

This solves the RErendering as Stringify conditions work only when sequence is maintained, firestore is Sending data in random order at times, So avoid spread operaetor here would save 50k firebase calls for you.

0reactions
vp5hcommented, Sep 18, 2021

Something is causing it to render projects again and again is there a possibility of circular dependency here

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introducing Hooks - React
Classes confuse both people and machines. In addition to making code reuse and code organization more difficult, we've found that classes can be...
Read more >
Understanding common frustrations with React Hooks
React Hooks can be frustrating despite their popularity and widespread use. Learn about some of the drawbacks to using React Hooks.
Read more >
Confused with rules · Issue #18 · Gelio/tslint-react-hooks
I'm confused with some rules. ... My code is w... ... Hooks can only be used inside function components and custom hooks.
Read more >
React hook confusion - Stack Overflow
Here a little mock up of your code which seems to do whats expected, so I would check the return value of your...
Read more >
Why hooks are the best thing to happen to React
Class components have always been a little cumbersome and confusing, especially as it makes state management and code reuse look so much ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found