简体   繁体   English

如何将 map 索引传递给由 jsx 元素组成的数组项?

[英]How to pass map indexes to array items compose of jsx element?

I had this code where I could not pass the key index of the array item map.我有这段代码,我无法传递数组项 map 的键索引。

arrayItem = [function1, function2, function3]

<div>
   {arrayItem.map((item, index) => (<Box key={index}>{item}</Box>}
</div>

ex.前任。 for function1对于功能1

const function1 = (index) => { <div> <h1> test</h1> <Button onClick((index) => console.log(index,"array number")}

It seems that I could not retrieve the index from the map when it goes to function1.当 map 转到 function1 时,我似乎无法检索索引。 The index according the console in the map is numeric but when it goes to the function, it is a className {} .根据 map 中的控制台的索引是数字,但是当它转到 function 时,它是一个className {} The purpose of this is to retrieve the number of the array so that I could put the button delete and delete the specific array according to the index.这样做的目的是检索数组的编号,以便我可以将按钮删除并根据索引删除特定的数组。 Btw, the arrayItems is not static, so it can have more than one functions.顺便说一句,arrayItems 不是 static,所以它可以有多个功能。

Thanks for the help.谢谢您的帮助。

If you consider your items as just normal functions you can pass index as parameter to these function and then call them like this如果您认为您的项目只是普通功能,您可以将index作为参数传递给这些 function,然后像这样调用它们

<div>
   {arrayItem.map((item, index) => (<Box key={index}>{item(index)}</Box>))}
</div>

also you have some mistakes in your functions, you need to return some jsx from it.你的函数也有一些错误,你需要从中return一些jsx and you have a typo in onClick((... . try this:并且您在onClick((... . 试试这个:

const function1 = (index) => {
  return (
    <div>
      <h1> test</h1>
      <Button onClick={(index) => console.log(index, "array number")}>click me</Button>
    </div>
  );
};

There is another senario that you consider your arrayItem an array of React Function Component .还有另一种情况,您认为您的arrayItemReact Function Component的数组。 in that case you need change your code to this and pass index as a prop:在这种情况下,您需要将代码更改为此并将index作为道具传递:

<div>
   {arrayItem.map((FunctionComponent, index) => (<Box key={index}><FunctionComponent index={index} /></Box>))}
</div>

and then use the index like this:然后像这样使用index

const function1 = ({index}) => {
  return (
    <div>
      <h1> test</h1>
      <Button onClick={(index) => console.log(index, "array number")}>click me</Button>
    </div>
  );
};

React keys are special and not treated as any part of props , similarly as-is with React refs . These are not exposed to the children components. If you need to pass an React 键是特殊的,不被视为props的任何部分,与 React refs 类似. These are not exposed to the children components. If you need to pass an . These are not exposed to the children components. If you need to pass an . These are not exposed to the children components. If you need to pass an index` value to a child it needs to be passed as a prop. . These are not exposed to the children components. If you need to pass an index` 值传递给孩子,则需要将其作为道具传递。

arrayItem = [item1, item2, item3]

<div>
  {arrayItem.map((item, index) => (
    <Box
      key={index}
      index={index} // <-- pass as a valid prop
    >
      {item}
    </Box>
  )}
</div>

Seems you are wanting to receive the index as a prop, then it needs to be accessed from props .似乎您希望将index作为道具接收,然后需要从props访问它。

const function1 = (props) => (
  <div>
    <h1>test</h1>
    <Button onClick(() => console.log(prop.index, "array number")
  ...
)

Now it seems that you have an array of functional components and you want to pass the index to that component.现在看来您有一个功能组件数组,并且您想将索引传递给组件。 In this case you need to render it as JSX and not just as a passed function.在这种情况下,您需要将其呈现为 JSX 而不仅仅是传递的 function。

<div>
  {arrayItem.map((Component, index) => ( // <-- give proper PascalCased component name
    <Component
      key={index}
      index={index} // <-- pass as a valid prop
    />
  )}
</div>

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

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