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

274
Visualizações
Uncaught TypeError: Cannot read properties of undefined (reading 'common')
   import React, { useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';

const CountryDetail = () => {
    const {countryname}=useParams();
    const[countrydet,setCountry]=useState([]);
    useEffect(()=>{
        const url=`https://restcountries.com/v3.1/name/${countryname}`;
        fetch(url)
        .then(res=>res.json())
        .then(data=>setCountry(data))
    },[])
    return (
        <div style={{textAlign: 'center'}}>
            
            <h3>This is country details : {countryname}</h3>
            <h4>{countrydet.name.common}</h4>
            <h4>{countrydet.continents}</h4>
            
        </div>
    );
};

export default CountryDetail;

but there is a element in object {"name":{"common":"Bangladesh","official":"People's Republic of Bangladesh","nativeName":{"ben":{"official":"বাংলাদেশ গণপ্রজাতন্ত্রী","common":"বাংলাদেশ"}}}

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

0

When the component first renders, the asynchronous API call and setting of state hasn't happened yet, so countrydet is undefined.

            <h3>This is country details : {countryname}</h3>
            <h4>{countrydet?.name?.common}</h4>
            <h4>{countrydet?.continents}</h4>

This should work, and here's the MDN reference for optional chaining.

or alternatively, you could wrap the whole block in a conditional, something like this:

            <h3>This is country details : {countryname}</h3>
            { countrydet?.name?.common && countrydet?.continent ? 
               <>
                 <h4>{countrydet?.name?.common}</h4>
                 <h4>{countrydet?.continents}</h4>
               </>
               :
               <></>
             }
about 4 years ago · Juan Pablo Isaza Relatório

0

  • Firstly, set an initial state for countrydet .
  • Then correctly unwrap the response from the fetch request. .then(([data]) => setCountry(data));
  • Check if the countryname parameter is set before executing the fetch request.
  • Check if countrydet is defined before trying to access it's properties.

const CountryDetail = () => {
  const { countryname } = useParams();
  const [countrydet, setCountry] = useState(undefined);

  useEffect(() => {
    if (countryname) {
      const url = `https://restcountries.com/v3.1/name/${countryname}`;
      fetch(url)
        .then((res) => res.json())
        .then(([data]) => setCountry(data));
    }
  }, [countryname]);

  return (
    <div style={{ textAlign: "center" }}>
      <h3>This is country details : {countryname}</h3>
      {countrydet && (
        <>
          <h4>{countrydet.name.common}</h4>
          <h4>{countrydet.continents}</h4>
        </>
      )}
    </div>
  );
};

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