Spencer Brereton
Spencer Brereton

Reputation: 51

React Webpage only re-renders when I change some code

I've got a web application using React. I've been using the useEffect hook to perform a database call on first render, but 90% of the time the code in the useEffect hook never fires, and the 10% of time it does, I can't figure out why. But if I change literally anything in that script (even a console.log) it is guaranteed to fire 100% of the time.

This is how I'm using useEffect to only fire once since its a database read.

    useEffect(() => {
        performAction()
      },[])

Am I fundamentally misunderstanding how page renders work here?

Upvotes: 1

Views: 39

Answers (1)

Jan Arend
Jan Arend

Reputation: 324

you can just pass the database object/function into the (watch) spot of the useEffect hook! just type your database const or database read function into the square brackets at the end of the useEffect() function!

something like this:

useEffect(() =>{performAction(), [yourDatabaseReadFunction()]}

Upvotes: 1

Related Questions