【问题标题】:Using the Map Function in a React Component在 React 组件中使用 Map 函数
【发布时间】:2021-11-18 10:36:21
【问题描述】:

我有一个 React 组件。它加载一个本地 JSON 文件。在构造函数中,我想遍历本地 JSON 文件,找到与 URL 参数匹配的项目,并设置一些状态值。到目前为止,这是我的代码:

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;

我不断收到此错误:Array.prototype.map() 需要箭头函数的返回值

map 函数必须返回一个值吗?如果我没有返回值,我应该只使用 for 循环吗?我可以返回 JSON 块然后在地图之后设置状态吗?这样做的正确方法是什么?

编辑:将页面更改为主题

【问题讨论】:

  • 是的,你会使用forEach 或其他东西来代替map,但我认为这个用例也不应该使用。更好的方法是使用find 数组方法来获取匹配的topic,然后只设置一次状态。
  • 你能把你的 JSON 的结构也发一下吗
  • 除非你想产生一个新的数组,否则我不推荐使用 map 作为你的迭代器。另外,这里的page 变量是什么?我没有看到它在任何地方声明。
  • @DanZuzevich,你是对的。可能是topic.page.url

标签: javascript reactjs json return map-function


【解决方案1】:

虽然 find 在大多数情况下都可以使用,但由于我没有提供的原因,它不会。我最终在构造函数中使用了一个 forEach 循环。

constructor(props){
  super(props);

  topics.forEach((topic) => {

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

谢谢大家!

【讨论】:

    【解决方案2】:

    我的强烈建议有两个:

    • 使用find 方法查找正确的主题。
    • 不要使用状态来冗余存储您已经从道具中获得的信息。存储您已有的信息可能会导致分歧。在这种情况下,我们可以在 render 方法中 find 适当的主题:
    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;
    

    【讨论】:

    • 这行得通! +1,但是@Nick,它更贵吗?
    • page 变量来自哪里?
    • @smilyface 我想这比在 mount 上做更贵,但这只是因为它不会对 prop 更新产生反应,这看起来很糟糕!
    • @DanZuzevich 我也有这个问题;我的猜测是 OP 遗漏了一些问题。
    • @smilyface 我认为认识到 99.999% 的 React 应用不必担心缓存 find 调用等优化也很重要。当然可以,但我宁愿有一个渲染变量而不是冗余状态,并且只有在真正需要时才跨过优化桥。
    【解决方案3】:

    没有看到您的 JSON 结构,很难回答


    将 {} 更改为 () 可能会起作用

    map(() =&gt; {})map(() =&gt; ())

    Array.prototype.map() 期望箭头函数 array-callback-return react 的返回值


    另一个答案

    尝试使用forEach(将map 替换为forEach)我猜您的JSON 不会返回数组。

    【讨论】:

    • 我使用的实际 JSON 是主题列表,主题中包含文章。我没有发布它,因为一旦我有了迭代 JSON 的最佳方法,我想我可以从那里拿走它。
    猜你喜欢
    • 2018-01-16
    • 2018-05-19
    • 2018-09-06
    • 2018-01-08
    • 2020-10-27
    • 2016-09-06
    • 2021-12-30
    • 2019-10-04
    • 1970-01-01
    相关资源
    最近更新 更多