【问题标题】:React 16 iterating over map rendering keyReact 16 迭代地图渲染键
【发布时间】:2018-05-05 23:41:26
【问题描述】:

我正在尝试遍历 Immutable.js 映射以呈现组件,但是虽然这是在呈现,但它也在呈现页面的键。我不知道为什么。

render() {
    const myMap = new Map({foo: '', bar: 'http://google.com/'})
    const {showFoo, titleAndUrl} = this.props
    return (
      <ul style={[
        styles.container,
        showFoo && styles.container.inline,
      ]}>
        {myMap.map((title, url) => this.renderTab(url, title))}
      </ul>
    )
  }

  renderTab(title, url) {
    const {showFoo, isFoo} = this.props
    return (
      <li key="sb" style={[
        styles.listItem,
        showFoo && styles.listItem.inline,
      ]}>
        <a
          href={url}
          key={title}
          style={[
            styles.link,
            styles.activeLink,
            showFoo && styles.link.inline,
          ]}
          className={isFoo ? "style" : ''}>
          {title}
        </a>
      </li>
    )
  }
}

两个名称和 url 被正确呈现,但是重复的键被呈现,即 foo 被呈现两次,bar 也是如此,但是 foo 和 bar 键之一没有样式,这表明它被呈现在 this.renderTab

看图:

呈现的 HTML:

<ul data-radium="true"
    style="display: flex; align-items: center; padding: 0px; margin: 0px; list-style: none; width: 100%; border-top: 1px solid rgb(221, 221, 221); height: 48px;">
    foo
    <li data-radium="true" style="display: flex; width: 50%; height: 47px; cursor: default;"><a href=""
                                                                                                class=""
                                                                                                data-radium="true"
                                                                                                style="display: flex; justify-content: center; align-items: center; width: 100%; text-decoration: none; font-weight: 500; font-size: 16px; color: rgb(0, 31, 91); transition: color 0.1s linear;">foo</a>
    </li>
    bar
    <li data-radium="true" style="display: flex; width: 50%; height: 47px; cursor: default;"><a
            href="http://google.com" class="" data-radium="true"
            style="display: flex; justify-content: center; align-items: center; width: 100%; text-decoration: none; font-weight: 500; font-size: 16px; color: rgb(0, 31, 91); transition: color 0.1s linear;">bar</a>
    </li>
</ul>

【问题讨论】:

  • map() 返回一个列表,它不仅循环遍历元素。这可能与某种原因有关。但我不确定为什么它会返回密钥而不是整个组件。

标签: javascript reactjs immutable.js


【解决方案1】:

您混淆了参数的顺序,将title 分配给url,反之亦然。

另外,传递给 Immutable.Map.map 的回调函数的参数是 (1) value,(2) key,所以第一个参数是你的 URL,第二个是你的标题。

将调用的行更改为map,如下所示:

{myMap.map((url, title) => this.renderTab(title, url))}

另一个问题是,你正在渲染的列表项元素都有相同的键“sb”,只是“a”元素的键发生了变化,但这甚至不需要。

renderTab返回的JSX改成这样:

  <li key={title} style={[
    styles.listItem,
    showFoo && styles.listItem.inline,
  ]}>
    <a
      href={url}
      style={[
        styles.link,
        styles.activeLink,
        showFoo && styles.link.inline,
      ]}
      className={isFoo ? "style" : ''}>
      {title}
    </a>
  </li>

最后,主要的错误是你期望Immutable.Map.map返回一个数组,但它没有,它返回另一个不可变映射,所以你必须使用@987654321将map函数返回的值转换为一个数组@ 和toArray

所以你的 map 语句实际上应该是这样的:

{myMap.map((url, title) => this.renderTab(title, url)).valueSeq().toArray()}

see related post on Stack Overflow

【讨论】:

  • 由于某种原因,按该顺序使用参数是不正确的,我希望标题是键,而 url 是值,但是这样做的时候正好相反。但是,它仍在渲染重复的键
  • 查看 immutable 的文档后更新了我的答案
  • 好的,这看起来不错,但是仍然呈现重复的键
  • 添加了一张图片,以便您了解我的意思
  • 是的,我明白了。能否包含实际呈现的 HTML 代码?
猜你喜欢
  • 2018-03-31
  • 2015-11-03
  • 2022-08-12
  • 2018-05-14
  • 2018-07-21
  • 1970-01-01
  • 1970-01-01
  • 2019-10-06
  • 1970-01-01
相关资源
最近更新 更多