在React中循环遍历对象的简单数组 [英] Loop through simple array of objects in React

查看:50
本文介绍了在React中循环遍历对象的简单数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用JSX.这有问题吗?这被认为是不好的做法吗?

I am not using JSX. Is this a problem? Is this considered bad practice?

var links = [
  { endpoint: '/america' },
  { endpoint: '/canada' },
  { endpoint: '/norway' },
  { endpoint: '/bahamas' }
];

class Navigation extends React.Component {
  render() {
    return (
      <div className="navigation">
        <ul>
          const listItems = links.map((link) =>
            <li key={link.endpoint}>{link.endpoint}</li> 
          );
        </ul>
      </div>
    );
}

基于react docs的基本列表组件部分,看来我应该能够打印数组的内容,就像我在< ul></ul>内一样;

Based on the basic list component section of the react docs, it seems like I should be able to print the contents of an array, the way I'm doing it inside my <ul></ul>

https://facebook.github.io/react/docs/lists-and-keys.html#basic-list-component

问题是我正在使用对象数组吗?该文档正在使用一个简单的数组.我希望朝正确的方向前进.

Is the problem that I am using an array of objects? The docs are using a simple array. I'd appreciate a push into the right direction.

推荐答案

问题是您的语法无效,您应该输入以下内容:

The issue is that your syntax is invalid, you should have something like this :

var links = [
  { endpoint: '/america' },
  { endpoint: '/canada' },
  { endpoint: '/norway' },
  { endpoint: '/bahamas' }
];

class Navigation extends React.Component {
  render() {
    const listItems = links.map((link) =>
        <li key={link.endpoint}>{link.endpoint}</li> 
    );
    return (
      <div className="navigation">
        <ul>
          {listItems}
        </ul>
      </div>
    );
}

这篇关于在React中循环遍历对象的简单数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆