【问题标题】:Returning multiple HTML snippets per element of array为数组的每个元素返回多个 HTML 片段
【发布时间】:2019-12-07 18:44:54
【问题描述】:

已编辑--

假设我有一个 JSON 对象数组:

"social":[
      {
        "name":"facebook",
        "className":"fa fa-facebook"
      },
      {
        "name":"linkedin",
        "className":"fa fa-linkedin"
      },
      {
        "name":"instagram",
        "className":"fa fa-instagram"
      },
      {
        "name":"github",
        "className":"fa fa-github"
      }
]

如何为每个对象创建一个 sn-p 以便它们返回

<p>{social.name}<p> 

而且我不想使用地图。

这是一个更复杂的例子,但这似乎是我面临的问题(即我有下面格式的数据,我需要从每个元素中获取属性来显示,我只有一个功能)

【问题讨论】:

  • 您介意创建一个可重现的最小示例 CodeSandbox 吗?
  • 在您提供的代码快照中看不到任何.length 方法,如果您发布原始错误代码会很棒。
  • 嗨,苏丹,我的代码中没有任何 .length 方法...它来自默认的地图方法
  • @SiddAjmera 嗨,Sidd,我已将问题编辑得更笼统。你能帮我解决这个问题吗?

标签: javascript html reactjs typeerror


【解决方案1】:

假设social是状态的一部分,你可以实现一个方法,将social数组中的每一项映射到p标签:

renderSocialNames = () => {
  return this.state.social.map(
    socialItem => <p key={socialItem.className}>{socialItem.name}</p>
  );
}

这里有一个Working Sample StackBlitz 供您参考。

【讨论】:

  • 如果我想要映射中的多行,即

    {socialItem.name}

    还在工作吗?
  • 是的,应该。只要您从 map 操作的每次迭代中返回一个有效的 jsx 元素,就没有理由不这样做。
【解决方案2】:

更简洁的代码:),这可能会解决您的问题

import React, { Component } from "react";

class Projects extends Component {
  constructor(props) {
    //initialize this component with props
    super(props);
  }

  render() {
    const { data } = this.props;
    if (data) {
      const projects = data.map(project => {
        return (
          <a className="cell" data-remodal-target={project.id}>
            <img
              className="grid-image"
              src={project.cover}
              data-aload={projects.cover}
              alt={project.name}
            />
          </a>
        );
      });

      const modals = data.map(project => {
        return (
          <div className="remodal" data-remodal-id={project.id}>
            <button
              data-remodal-action="close"
              className="remodal-close"
            ></button>
            <h1>Remodal</h1>
            <p>
              Responsive, lightweight, fast, synchronized with CSS animations,
              fully customizable modal window plugin with declarative
              configuration and hash tracking.
            </p>
            <br />
            <button data-remodal-action="cancel" className="remodal-cancel">
              Cancel
            </button>
            <button data-remodal-action="confirm" className="remodal-confirm">
              OK
            </button>
          </div>
        );
      });
    }

    return (
      <section id="projects">
        <div className="grid-container remodal-bg">
          {projects}
          {modals}
        </div>
      </section>
    );
  }
}

【讨论】:

  • 嗨 Aashish,我试过了。仍然不起作用 - 因为它说项目不存在(即数据为空,但当我不使用地图时数据显然不为空)
  • 嘿@Will,添加数据? data.map(这里是相同的项目代码):[];很可能数据可能尚未初始化,因此无法使用,因此更好的选择是将默认后备值设为空数组
  • 没用。你能帮我解决上面的问题吗?我进行了编辑以使其更通用
  • @Will,可以使用 for 循环
猜你喜欢
  • 2014-07-30
  • 2021-08-09
  • 1970-01-01
  • 1970-01-01
  • 2019-01-07
  • 2014-12-14
  • 2021-04-01
  • 1970-01-01
  • 2011-03-02
相关资源
最近更新 更多