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

366
Visualizações
How to declare json data fields that I am getting in an api in ReactJs

enter image description here

As can be seen from the image, I am getting error lines under element.url etc. from line no. 62. I am fetching data from news api and in result I am getting json response which looks like this. I am passing these title, description etc. from api response to another component in my react app as props.

enter image description here

Now the error says this :

any | Property 'title' does not exist on type 'never'.ts(2339)

So my question is; is there a way I can declare the rest of the fields of the json data so my editor doesn't give this error? Here is my code....

constructor(props) {
    super(props);
    this.state = {
        articles: [],
        totalResults: 0,
        loading: false,
        page: 1,
    };
}

async componentDidMount() {
    let apiKey = `https://newsapi.org/v2/top-headlines?country=in&apiKey=7e87c89bfbbd4b8b8bb0721d45a6d454&pageSize=18&page=${this.state.page}`;
    let data = await (await fetch(apiKey)).json();
    this.setState({ articles: data.articles, totalResults: data.totalResults });
}

render() {
    return (
        <div className="container my-4">
            <h2 id="heading">Top headlines of the day</h2>
            <div className="row my-4">
                {this.state.articles.map((element) => {
                    return (
                        <div className="col-md-4 mb-3" key={element.url}>
                            <NewsItem
                                title={element.title}
                                description={element.description}
                                imageUrl={element.urlToImage}
                                more={element.url}
                            />
                        </div>
                    );
                })}
            </div>
about 4 years ago · Santiago Gelvez
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