Leo
Leo

Reputation: 1

How to call firebase hook after some delay in react?

I am adding new users. On backend it takes about 2 second to give me access to new user data which I have created. The cause of the error is access control.When I try to get user using firebase hook it gives me an error :

Error: permission_denied at /users/M2uQPQRacJZxjCwxqVT0UlHcwy72: Client doesn't have permission to access the desired data.

After refreshing the page I am able to access the data of the new user and it is shown in list.

I would like to know is there any way to call firebase hook after some seconds so that I will not get this error?

Firebase hook:

 export const useUser = userId => {
   if (!userId) {
      return [null, false]
   }
   const ref = firebase.database().ref(`users/${userId}`)
   return useObjectVal(ref)
 }

usage:

import { useUser } from '../../hooks/firebase-hooks'

function TeamMemberListItem(props) {

  const userId = props.userId
  const [user, loading, error] = useUser(userId)

}

Upvotes: 0

Views: 260

Answers (1)

VersifiXion
VersifiXion

Reputation: 2282

I don't think I did understand, but did you try with a useEffect ? maybe if you use useEffect with userId as second parameter ?

import React, { useEffect } from 'react';

useEffect(() => {
 // do something when the component is mounted or userId is updated

 setInterval(() => {
   // do something 2 seconds after the component is mounted or userId is updated
 }, 2000);
}, [userId]);

Upvotes: 1

Related Questions