<div id='root' ></div>
body {
  font-family: Arial, sans-serif;
  background-color: #1a1d1d;
  padding: 20px;
  font-size: 0.875rem;
}

button {
  background-color: #4caf50;
  color: white;
  padding: 10px 15px;
  margin: 5px 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #45a049;
}

select {
  padding: 10px 15px;
  margin: 5px 5px;
  background-color: #f9f9f9; 
  border: 1px solid #ddd;
  border-radius: 4px; 
  font-size: 0.875rem; 
  cursor: pointer; 
  outline: none; 
}

select:hover {
  background-color: #e9e9e9; 
}

select:focus {
  border-color: #4caf50; 
  box-shadow: 0 0 3px #4caf50; 
}

.container {
  background-color: #fff;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  max-width: 30rem;
  margin: auto;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  background-color: #f9f9f9;
  margin: 8px 0;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.list-item div {
  margin-right: 10px;
}

.controls-container {
  display: flex;
  justify-content: space-evenly;
  margin-bottom: 8px;
  border: solid 1px
}

@media screen and (max-width: 768px) {
  
  body {
    padding: 5px;
  }
  
  .container {
    padding: 5px; 
    width: 100%; 
  }
}
import React, {
  useState,
  useRef,
  useEffect
} from "https://esm.sh/react@18.2.0";
import ReactDOM from "https://esm.sh/react-dom@18.2.0";

const App = () => {
  const [keyType, setKeyType] = useState("index");
  return (
    <>
      <div className="container">
        <label>
          Select a key type:
          <select
            onChange={(e) => {
              setKeyType(e.target.value);
            }}
          >
            <option value="">None</option>
            <option value="index" selected>
              Index
            </option>
            <option value="unique">Unique</option>
          </select>
        </label>
        <KeyListDemo keyType={keyType} />
      </div>
    </>
  );
};

const Item = ({ name }) => {
  const [counter, setCounter] = useState(0);

  useEffect(() => {
    setCounter(1);
  }, []);
  return (
    <span className="list-item">
      <div>
        Name: {name}, Count: {counter}
      </div>{" "}
      <button
        type="button"
        onClick={() => {
          setCounter(1 + counter);
        }}
      >
        {" "}
        increment counter
      </button>
    </span>
  );
};

const KeyListDemo = ({ keyType }) => {
  const initialItems = [
    { id: 198, name: "Apple" },
    { id: 231, name: "Banana" },
    { id: 123, name: "Cherry" },
    { id: 653, name: "Date" },
    { id: 109, name: "Elderberry" }
  ];
  const [items, setItems] = useState(initialItems);

  const shuffleItems = () => {
    const newItems = [...items];
    newItems.sort(() => Math.random() - 0.5);
    setItems(newItems);
  };

  const addItem = () => {
    const newItemName = prompt("Enter a new fruit:");
    if (newItemName) {
      const newItem = { id: new Date().getTime(), name: newItemName };
      setItems([...items, newItem]);
    }
  };

  const getKey = (item, index) => {
    switch (keyType) {
      case "index":
        return index;
      case "unique":
        return item.id;
      default:
        return "";
    }
  };

  return (
    <>
      <div className="controls-container">
        <button onClick={shuffleItems}>Shuffle Items</button>
        <button onClick={addItem}>Add Item</button>
      </div>
      <div>Current state: {JSON.stringify(items)}</div>
      <ul>
        {items.map((item, index) => {
          // Correct placement of the JavaScript expression
          const identifier = getKey(item, index);
          console.log("identifier", identifier);
          return (
            <li key={identifier}>
              index: {index}
              <br />
              key: {identifier !== "" ? identifier : "none"}
              <Item name={item.name} />
            </li>
          );
        })}
      </ul>
    </>
  );
};

ReactDOM.render(<App />, document.getElementById("root"));
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.