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

188
Visualizações
Get data using useeffect hook in react js

I have a simple page editor, When a user clicks edit page it opens an editor. I am passing the ID of the page using redux which will be used to get data from API.

Here is my Editor.

const [pageData, setPageData] = useState("");

  const getPage = async (id) => {
    try {
      const response = await api.get(`/landing_pages/${id}`);
      console.log("page", response.data); // displays data at the end
      setPageData(response.data);
    } catch (error) {
      console.log(error);
    }
  };

  useEffect(() => {

    getPage(pageID);

    console.log('Page Data', pageData) // displays nothing

    let LandingPage = pageData;

     const editor = grapesjs.init({
       container: "#editor",
       components: LandingPage.components || LandingPage.html,
     })

  }, [pageID, getPage])

Why is Page Data display nothing even though the data from API is returned and is displayed in the console at the end? what am I doing wrong here?

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

0

Even if you await your getPage call, the updated pageData won't be available until the next render cycle so your assignment to LandingPage will be one cycle behind.

You should instead update in one useEffect and watch for changes to pageData in another.

const [pageData, setPageData] = useState("");

  useEffect(() => {
    const getPage = async (id) => {
      try {
        const response = await api.get(`/landing_pages/${id}`);
        console.log("page", response.data); // displays data at the end
        setPageData(response.data);
      } catch (error) {
        console.log(error);
      }
    };

    getPage(pageID);
  }, [pageID]);

  useEffect(() => {
    console.log('Page Data', pageData); // displays updated pageData

    let LandingPage = pageData;

    const editor = grapesjs.init({
      container: "#editor",
      components: LandingPage.components || LandingPage.html,
    });
  }, [pageData]);
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