【问题标题】:Rendering ReactJS component based on a list of objects基于对象列表渲染 ReactJS 组件
【发布时间】:2017-11-06 23:04:06
【问题描述】:

我有以下组件:

articles_list.jsx

import React from 'react';
import './articles_list.css';

export default class ArticlesList extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      articles: null
    }
  }

  componentWillMount() {
    fetch('/articles_all')
    .then(res => res.json())
    .then(json => {
      this.setState({
        articles: json.articles
      });
    });
  }

  render() {

    var teste = () => {
      if (this.state.articles === null) {
        return(<div>No articles</div>)
      } else {
          {this.state.articles.forEach( function(element, index) {
            console.log(element);
            return <div key={index}>{element.title}</div>;
          })}
      }
    }

    return(
      <div className="articles_list">
        <div className="articles_list_title">
          ARTICLES
        </div>
        <div>{teste()}</div>
      </div>
    );
  }
}

虽然 JSON 请求工作正常并返回一个包含五个 JSON 对象的数组,但它们只是不渲染!

我是 ReactJS 的新手,阅读(并观看)了很多教程,但我似乎遗漏了一些东西。

有什么建议吗?

【问题讨论】:

  • Array.prototype.forEach 什么都不返回,你想要Array.prototype.map 代替。

标签: javascript json reactjs


【解决方案1】:

forEach 中的return 语句不返回值,而是像continue 语句一样,你需要使用map

var teste = () => {
  if (this.state.articles === null) {
    return(<div>No articles</div>)
  } else {
      {this.state.articles.map( function(element, index) {
        console.log(element);
        return <div key={index}>{element.title}</div>;
      })}
  }
}

如果你想使用 forEach,你需要修改你的代码

render() {

    var teste = []

      if (this.state.articles === null) {
        teste.push(<div>No articles</div>)
      } else {
          {this.state.articles.forEach( function(element, index) {
            console.log(element);
            teste.push( <div key={index}>{element.title}</div>);
          })}
      }
    }

    return(
      <div className="articles_list">
        <div className="articles_list_title">
          ARTICLES
        </div>
        <div>{teste}</div>
      </div>
    );
  }

【讨论】:

  • 成功了,谢谢!但我仍然收到一个奇怪的警告:Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of ArticlesList. 似乎很奇怪,因为我在 div 中使用了key={index}。
  • 在这种情况下,请尝试为&lt;div&gt;No articles&lt;/div&gt; 提供密钥
  • 谢谢!现在一切都很好!
  • 很高兴有帮助:)
  • 我不想猜测你的好意,但你能看看这个吗? stackoverflow.com/questions/44382657/…
【解决方案2】:

你可以试试这样的。

import _ from 'lodash';
renderArticles() {
  return _.map(this.state.articles, article => {
    return (
      <li className="list-group-item" key={article.id}>
          {article.title}
      </li>
    );
  });
}


  render() {
    return (
      <div>
        <h3>Articles</h3>
        <ul className="list-group">
          {this.renderArticles()}
        </ul>
      </div>
    );
  }

映射列表并一一呈现。在这里,我使用 lodash 地图助手来完成这项工作。希望这可以帮助。编码愉快。

【讨论】:

  • 一旦拥有Array.prototype.map,真的没有理由使用_.map
猜你喜欢
  • 2018-11-02
  • 2016-02-11
  • 2021-03-08
  • 2021-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 2017-10-23
相关资源
最近更新 更多