0

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

4
  • 1
    Yes, you would use forEach or something instead of map, but I don't think this use case should use either. A better way to do it would be to use a find array method to get the one matching topic and then just set the state once. Commented Sep 25, 2021 at 14:13
  • Can you also post the structure of your JSON Commented Sep 25, 2021 at 14:14
  • 1
    Unless you want to produce a new array, I don't recommend using map as your iterator. Also, What is the page variable here? I don't see it declared anywhere. Commented Sep 25, 2021 at 14:26
  • @DanZuzevich, You're right. May be topic.page.url Commented Sep 25, 2021 at 14:30

3 Answers 3

3

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;
Sign up to request clarification or add additional context in comments.

8 Comments

This would work! +1 , but @Nick, is it more costly?
Where is the page variable coming from?
@smilyface I guess it's more expensive than doing it on mount but only because it's going to not be reactive to prop updates which would seem pretty bad!
@DanZuzevich I had that question too; my guess is OP left something out of the question.
@smilyface I think it's also important to recognize that 99.999% of react apps will not have to worry about optimizations like caching find calls. Sure, it can be done, but I would much rather have a render variable than redundant state and only cross the optimization bridge if truly necessary.
|
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!

Comments

-1

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.

1 Comment

The actual JSON I'm using is a list of topics with articles inside the topics. I didn't post it because once I had the best way to iterate through my JSON I figured I could take it from there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.