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

129
Visualizações
Using the Map Function in a React Component

I have a React component. It loads a local JSON file. In the constructor I want to loop through the local JSON file, find an item that matches a URL parameter, and set some state values. Here is my code so far:

import React,{Component} from "react";

import topics from './topics.json';

class Tutorial extends Component {

  constructor(props){
    super(props);

    topics.map((topic, index) => {

      if(topic.url === this.props.match.params.url)
      {
        this.state = {
          url: this.props.match.params.url,
          name: topic.name
        };
      }
    })
  }

  render() {
    return (
      <div className="Tutorial">

        <div className="ca-nav-spacer w3-hide-small"></div>

          {this.state.name}
          {this.state.url}

      </div>
    );
  }
}

export default Tutorial;

I keep getting this error: Array.prototype.map() expects a return value from arrow function.

Must the map function return a value? If I'm not returning a value should I just use a for loop? Can I return the block of JSON and then set the state after the map? What would be the proper way to do this?

Edit: Changed page to topic

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

0

My strong recommendation is two-fold:

  • Use the find method to find the correct topic.
  • Don't use state to redundantly store information you already have from props. Storing information you already have can result in divergence. In this case, we can just find the appropriate topic in the render method:
import React, { Component } from "react";

import topics from "./topics.json";

class Tutorial extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    const selectedTopic = topics.find(
      (topic, index) => page.url === this.props.match.params.url
    );

    return (
      <div className="Tutorial">
        <div className="ca-nav-spacer w3-hide-small"></div>
        {selectedTopic.name}
        {this.props.match.params.url}
      </div>
    );
  }
}

export default Tutorial;
about 4 years ago · Juan Pablo Isaza Relatório

0

Although find would work in most situations, it won't for reasons I did not include. I ended up using a forEach loop in the constructor.

constructor(props){
  super(props);

  topics.forEach((topic) => {

    if(topic.url === this.props.match.params.url)
    {
      this.state = {
        url: topic.url,
        title: topic.title
      }
    }
  })
}

Thanks everyone!

about 4 years ago · Juan Pablo Isaza Relatório

0

Without seeing your JSON structure, it's hard to answer


Changing {} to () might work

from map(() => {}) to map(() => ())

Array.prototype.map() expects a return value from arrow function array-callback-return react


Another answer

Try using forEach (replace map with forEach) I guess your JSON does not returns an array.

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