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

189
Visualizações
Setting document title after api call in React

I'm trying to set the document title after making an API call and getting a response. However, I get the following error:

Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'rendered')

I know why the error is occurring; It tries to access the API response (blogPost.title.rendered) before the response is returned. I'm just not sure why it's doing that and how to fix it. Here is my code:

import React, { useState, useEffect } from 'react';
import '../css/styles.css';
import { useParams, useLocation } from 'react-router-dom';
import dgAPI from '../cms/DGAPI';

const BlogPost = () => {
    const [loading, setLoading] = useState(true);
    const [blogPost, setBlogPost] = useState({});

    let blogPostURL = dgAPI.getPostFromSlug + useParams().slug;

    const getPost = async () => {
        // Fetch blog post
        fetch(blogPostURL)
            .then(response => response.json())
            .then(post => {
                setBlogPost(post[0]);
                setLoading(false);
            }).then(() => {
                document.title = blogPost.title.rendered;
            })
    }

    useEffect(() => {
        getPost();
    }, []);

    return (
        <>
            ...
        </>
    )
}

export default BlogPost

I have also tried the following but had no luck:

import React, { useState, useEffect } from 'react';
import '../css/styles.css';
import { useParams, useLocation } from 'react-router-dom';
import dgAPI from '../cms/DGAPI';

const BlogPost = () => {
    const [loading, setLoading] = useState(true);
    const [blogPost, setBlogPost] = useState({});

    let blogPostURL = dgAPI.getPostFromSlug + useParams().slug;

    const getPost = async () => {
        // Fetch blog post
        const response = await fetch(blogPostURL);
        const post = await response.json();
        setBlogPost(post[0]);
        document.title = blogPost.title.rendered;
        setLoading(false);
}

useEffect(() => {
    getPost();
}, []);

return (
    <>
        ...
    </>
)
}

export default BlogPost

I'm not sure why it won't wait to receive the response before it sets the document title.

Any help is much appreciated!

about 4 years ago · Juan Pablo Isaza
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