Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React useEffect issue… variable inside async function is getting a delayed updated value

Here is my code: I am trying to connect to supabase and i am getting a console 400 error twice but then it connects. It is due to userId variable, if i put the userId string i do not get the error. userId is being updated in a function just above this useEffect.

useEffect(() => {
async function example() {
try {

          const { data, error } = await supabase
          
            .from('profiles')
            .select()
            .eq('id', userId );
    
      };
     
      example();
    }, [userId]);

I added this at the bottom and it never connected.

if(userId) return: example()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any ideas what i can do to avoid any console errors. I cant use conditions on hooks to avoid running useEffect with an empty string. Do i need to setup useProviders to set the variable outside of the component, that way the userId variable is set before the component is ran?

>Solution :

You must to check userId value availability at the beginning of your hook, try following edition :

useEffect(async () => {
    if (!userId) return;
    const { data, error } = await supabase
            .from('profiles')
            .select()
            .eq('id', userId );
}, [userId]);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading