Sobes.tech
Middle

// === 1 === // Chiusura // TODO: implementa funzione id // console.log(id()); // 0; // console.log(id()); // 1; // console.log(id()); // 2; // console.log(id()); // 3; export default function App() { // TODO: implementa Table.tsx return <Table />; }

sobes.tech AI

Risposta dell'AI

Per implementare la funzione id, che restituisce un identificatore intero successivo ad ogni chiamata, puoi usare una closure per mantenere lo stato del contatore tra le chiamate:

function createIdGenerator() {
  let currentId = 0;
  return function id() {
    return currentId++;
  };
}

const id = createIdGenerator();

console.log(id()); // 0
console.log(id()); // 1
console.log(id()); // 2
console.log(id()); // 3

Per quanto riguarda il componente Table.tsx, senza requisiti specifici, puoi implementare un componente React semplice che mostra una tabella con dati fissi:

import React from 'react';

const data = [
  { id: 1, name: 'Alice', age: 30 },
  { id: 2, name: 'Bob', age: 25 },
  { id: 3, name: 'Charlie', age: 35 },
];

export default function Table() {
  return (
    <table>
      <thead>
        <tr><th>ID</th><th>Name</th><th>Age</th></tr>
      </thead>
      <tbody>
        {data.map(row => (
          <tr key={row.id}>
            <td>{row.id}</td>
            <td>{row.name}</td>
            <td>{row.age}</td>
          </tr>
        ))}
      </tbody>
    </table>
  );
}