简体   繁体   English

反应,使用 .map 和索引

[英]React, using .map with index

I have a simple React component like so:我有一个简单的 React 组件,如下所示:

import React from 'react';

const ListPage = ({todos}) => (
  <div>
    <h6>todos</h6>
    <ul>
    {todos.map(({todo, index}) => (
      <li key={index}>{todo}</li>
    ))}
    </ul>
  </div>
)

ListPage.propTypes = {
  todos: React.PropTypes.array,
};

export default ListPage;

I can see that the docs for Array.prototype.map() shows that the second argument is index, next to currentValue.我可以看到Array.prototype.map()的文档显示第二个参数是 index,在 currentValue 旁边。 How does one alter the existing code to pick up the second argument?如何更改现有代码以获取第二个参数?

You need to to this:你需要这样做:

todos.map((key, index) => { ... }) without object brackets for arguments. todos.map((key, index) => { ... })没有对象括号作为参数。

({todo, index}) => { ... } - that syntax means that you want to get properties todo and index from first argument of function. ({todo, index}) => { ... } - 该语法意味着您希望从函数的第一个参数中获取属性todoindex

You can see syntax here :您可以在此处查看语法:

Basic syntax基本语法

(param1, param2, …, paramN) => { statements } (param1, param2, …, paramN) => expression // equivalent to: => { return expression; } // Parentheses are optional when there's only one parameter: (singleParam) => { statements } singleParam => { statements } // A function with no parameters requires parentheses: () => { statements }

Advanced syntax高级语法

// Parenthesize the body to return an object literal expression: params => ({foo: bar}) // Rest parameters and default parameters are supported (param1, param2, ...rest) => { statements } (param1 = defaultValue1, param2, …, paramN = defaultValueN) => { statements } // Destructuring within the parameter list is also supported var f = ([a, b] = [1, 2], {x: c} = {x: a + b}) => a + b + c; f(); // 6

Here is a example base on @Timur Bilalov answer to make easy to understand这是一个基于@Timur Bilalov 答案的示例,以便于理解

var materials = [
  'Hydrogen',
  'Helium',
  'Lithium',
  'Beryllium'
];

materials.map((material,index) => console.log(index +" = " + material + " = " + materials[index]));

Output输出

"0 = Hydrogen = Hydrogen"
"1 = Helium = Helium"
"2 = Lithium = Lithium"
"3 = Beryllium = Beryllium"

Reference参考
https://reactjs.org/docs/lists-and-keys.html https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions https://reactjs.org/docs/lists-and-keys.html https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Hope it help希望有帮助

Be careful, you should not use the index as a key if the list is dynamic, it is antipattern and could lead into troubles.请注意,如果列表是动态的,则不应使用索引作为键,它是反模式的,可能会导致麻烦。

You should use the index as a key ONLY if you are sure you will not have to delete or add items to your todos after creation (it could still be acceptable if you add items but only at the end of the list, and still never delete).仅当您确定在创建后不必删除或添加项目到待办事项时,您才应该使用索引作为键(如果您添加项目但仅在列表末尾添加项目仍然可以接受,并且仍然永远不会删除)。 Otherwise React might run into problems reconciliating your children.否则,React 可能会遇到让孩子和解的问题。

The best practise is to add an "id" to all your todos (incremental or UUID) and use it as the key for all the react lists that need it.最佳实践是为所有待办事项(增量或 UUID)添加一个“id”,并将其用作所有需要它的反应列表的键。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM