简体   繁体   English

遍历数组并为每个项目返回一个`Dropdown.Item`元素

[英]Loop through the array and return a `Dropdown.Item` element for each item

I am trying to loop through the todos array using the JavaScript map() function. 我试图使用JavaScript map()函数遍历todos数组。 I want to return a Dropdown.Item element for each item. 我想为每个项目返回一个Dropdown.Item元素。 When I click the Dropdown.Toggle , an empty list expands. 当我单击Dropdown.Toggle ,一个空列表会展开。 Dropdown.Item and Dropdown.Toggle are react bootstrap components. Dropdown.ItemDropdown.Toggle是react引导程序组件。

Code here: https://stackblitz.com/edit/react-egxxcl 代码在这里: https : //stackblitz.com/edit/react-egxxcl

class App extends Component {
  constructor() {
    super();
    this.state = {
      todos: [{name: 'A', value: 10}, {name: 'B', value:20}]
    };
  }

  render() {
    return (
      <div>
       <Dropdown>
        <Dropdown.Toggle variant="success" id="dropdown-basic">
          Change
        </Dropdown.Toggle>

        <Dropdown.Menu >
          {this.state.todos.map((todo, index) => {
            <Dropdown.Item value={todo.value}>{todo.name}</Dropdown.Item>
          })}
        </Dropdown.Menu>
      </Dropdown>
      </div>
    );
  }
}

You don't return anything from the map function, you can wrap it in parentheses or without to avoid using the return keyword, also don't forget the key prop. 您不会从map函数返回任何内容,可以将其包装在括号中,也可以避免使用return关键字,也不要忘记key道具。

I highly recommend using a linter to help you catch those small errors 我强烈建议您使用皮棉机来帮助您捕获那些小错误

You can also destructure value and name from the map callback function using ({}) 您还可以使用({})从地图回调函数中解构值和名称。

({ propName })

<Dropdown.Menu>
  {this.state.todos.map(({ name, value }) => (
    <Dropdown.Item key={name} value={value}>{name}</Dropdown.Item>
  ))}
</Dropdown.Menu>

You just missed to return from map function. 您只是错过了从map功能return

{this.state.todos.map((todo, index) => {
     return <Dropdown.Item value={todo.value}>{todo.name}</Dropdown.Item>
})}

or shorten, 或缩短

{this.state.todos.map((todo, index) => (
   <Dropdown.Item value={todo.value}>{todo.name}</Dropdown.Item>
))}

or even shorten, 甚至缩短

{this.state.todos.map((todo, index) => <Dropdown.Item value={todo.value}>{todo.name}</Dropdown.Item>
)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对于循环中的每个项目,您将如何迭代并返回不同数组中的项目? - For each item in a loop, how would you iterate through and return an item in a different array? 在 React-bootstrap 中单击 Dropdown.Item 后防止关闭 - Prevent closing after click on Dropdown.Item in React-bootstrap 遍历每个项目的所有数组项目 - loop through all array items for each item 遍历数组,将每个项目添加到对象中,然后将其推入Javascript中的数组 - Loop through an Array, add each item to an object and push it to an array in Javascript 有没有更好的方法来循环遍历一个大数组来找到每个项目的数量? - Is there a better way to loop through a large array to find the number of each item? 如何循环遍历数组以等待每个项目之间的时间? - How to loop through an array in waiting a time between each item? Angular NgFor遍历数组对象,每个项目都有延迟 - Angular NgFor Loop Through Array Object with Delay for Each Item 通过for循环将元素添加到数组,然后返回最后一项 - Add element to array via for loop and then return the last item 在每个项目上循环遍历JS对象和函数 - Loop through JS Object and function on each item for循环返回数组中一个项的单个字母而不是数组中的每个项 - for loop returning individual letters of one item in array and not each item in the array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM