Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda