English 中文(简体)
能否在同一反应部分的单独地图功能中为不同的清单项目提供同样的钥匙?
原标题:Is it okay to have the same keys for different list items rendered within separate map functions in the same React component?
  • 时间:2024-02-14 00:10:31
  •  标签:
  • reactjs
  • jsx

Q: Is it okay to have the same keys for different list items rendered within separate map functions in the same React component?

<>Situation: 如你在实例代码中所示,将指数作为<代码>key识别码的Im。 两者都将从0、1、2、3开始。

(Both initialMessageData and messageData are dynamic, changing over time)

www.un.org/Depts/DGACM/index_spanish.htm 守则例:

  ...
  ...
  return (
    <ul>
      {initialMessageData.map((data, index) => (
        <ChatLog key={index} data={data} />
      ))}
      {messageData.map((data, index) => (
        <ChatLog key={index} data={data} />
      ))}
    </ul>
  );
  • If it is okay, why?
  • If it is not okay, why?
问题回答

From React sdocs:

Keys tell React which array item each component corresponds to, so that it can match them up later. This becomes important if your array items can move (e.g. due to sorting), get inserted, or get deleted. A well-chosen key helps React infer what exactly has happened, and make the correct updates to the DOM tree.

而且:

Keys must be unique among siblings. However, it’s okay to use the same keys for JSX nodes in different arrays.

为了回答你的问题,如果是这样的话,那是因为清单项目来自两个不同的阵列:Here,是供进一步阅读的同docs的链接。





相关问题
Validators shows for untouched input in Edit page (React)

Hi can anyone help me solve these validators problem. I have put validators for the input fields in an update page but the problem is, it shows validation to all the field that are not touched but the ...

Script that rotates a PNG in Photoshop?

I have the following Photoshop ExtendScript (.jsx) script which rotates the image canvas, but doesn t rotate the contents: var doc = app.open(...); // open the .png file if (doc.width > doc....

How to Process HTTP Response to Obtain Image Content

I m trying to integrate a web service that serves PNG images into Photoshop using JSX/ExtendScript. To accomplish this, I create a Socket (TCP) and write the user configurable parameters in the GET ...

热门标签