Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
Using Promise when extracting data from Supabase into React

I have an async function that gets data from my Supabase database, that when called, returns a promise with the correct data that I have queried, but when I try to call this function in a React component, I don't know how to extract the data from the Promise and just get the string that I queried.

I understand that you can not get the result of a promise in the same scope as you call it, but I'm not sure how I would get around this.

My code:

export async function getUserValue(uuid, value) {
    const { data, error } = await supabase
        .from('users')
        .select('username').eq("id", "8f1693d3-c6d9-434c-9eb7-90882ea6ef28"); // hard coded values for testing purposes
    return data;
}

Where I call it:

...
async function Sidebar(props) {
    console.log(getUserValue("", ""))

    return (
        <div className={"sidebar"}>
            <div className="sidebar-main">
                <img className={"sidebar-main-picture"} src={profile_picture} alt="pfp"/>
                <p className={"sidebar-main-name"}>Test</p>
...

Resultresult

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The way to store data in React components is to define and set state.

The correct place to handle side-effects like async response data is in an effect hook

import { useEffect, useState } from "react";

function Sidebar(props) {
  const [ user, setUser ] = useState(null); // initial value

  useEffect(() => {
    getUserValue("", "")
      .then(users => {
        setUser(users[0]) // your response is an array, extract the first value
      })
      .catch(console.error)
  }, []); // empty array means run this once on mount

  return user && ( // only display if `user` is set
    <p>Hello, { user.username }</p> {/* just an example */}
  );
}

I feel like this has definitely been asked and answered before but I couldn't find an applicable duplicate. Happy to remove this or mark it Community Wiki if somebody can link an existing post.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda