Zettelkasten
week01
Diference between Key and Id

#content


In React, both key and id are used to identify elements in a list, but they serve different purposes and have different uses:

  1. key:
    • The key property is specific to React and is used to help React uniquely identify each element in a list. Each element in a list should have a unique key.
    • The primary purpose of key is to optimize performance and rendering efficiency. When an element is added, removed, or rearranged in a list, React uses the key to understand which elements have changed and update only the relevant elements, rather than re-rendering the entire list.
    • key is typically used in React components when you map over a list to render elements.

Example of using key:

const friends = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' },
];
 
const friendList = friends.map((friend) => <div key={friend.id}>{friend.name}</div>);
  1. id:
    • The id property is a convention commonly used in HTML and many other contexts to provide a unique identification to an element.
    • In React components, you can use id in the same way as in any other context to identify an element, but it does not take advantage of React's internal optimizations for lists.
    • The use of id is not specific to rendering lists and is not used by React to optimize element updates in lists as key is.

Example of using id:

const friends = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' },
];
 
const friendList = friends.map((friend) => <div id={friend.id}>{friend.name}</div>);

Therefore, the main difference between key and id in React is that key is used specifically for list optimization and ensuring efficient rendering of elements in a list, while id is a general identifier that can be used for any purpose but does not provide the same rendering optimizations.