Powiązanie inputów z tablicą w React

Niech w stanie notes przechowywana jest tablica:

jsxfunction App() {
  const [notes, setNotes] = useState([1, 2, 3]);
  
  return <div>
    
  </div>;
}

Niech mamy również funkcję pomocniczą, znajdującą sumę elementów tablicy:

jsxfunction getSum(arr) {
  let sum = 0;
  
  for (const elem of arr) {
    sum += +elem;
  }
  
  return sum;
}

function App() {
  ...
}

Znajdźmy i wypiszmy sumę elementów naszej tablicy ze stanu, wykorzystując do tego naszą funkcję pomocniczą:

jsxfunction App() {
  const [notes, setNotes] = useState([1, 2, 3]);
  
  return <div>
    {getSum(notes)}
  </div>;
}

Zróbmy teraz trzy inputy i w value każdego inputa wpiszmy jeden z elementów tablicy:

jsxfunction App() {
  const [notes, setNotes] = useState([1, 2, 3]);
  
  return <div>
    <input value={notes[0]} />
    <input value={notes[1]} />
    <input value={notes[2]} />
    
    {getSum(notes)}
  </div>;
}

Dodajmy teraz zdarzenie onChange naszym inputom. Przy tym zróbmy jedną wspólną funkcję-obsługującą tego zdarzenia:

jsxfunction App() {
  const [notes, setNotes] = useState([1, 2, 3]);
  
  function changeHandler(index, event) {
    // wspólna funkcja-obsługująca
  }
  
  return <div>
    <input value={notes[0]} onChange={event => changeHandler(0, event)} />
    <input value={notes[1]} onChange={event => changeHandler(1, event)} />
    <input value={notes[2]} onChange={event => changeHandler(2, event)} />
    
    {getSum(notes)}
  </div>;
}

Jak widzicie, funkcja changeHandler pierwszym parametrem przyjmuje numer tego elementu tablicy, który edytuje dany input.

Po tym numerze możemy zamienić element tablicy na zawartość inputa.

Zróbmy to:

jsxfunction changeHandler(index, event) {
  setNotes([...notes.slice(0, index), event.target.value, ...notes.slice(index + 1)]);
}

Teraz będzie można edytować dowolny input, przy tym reaktywnie będzie zmieniać się tablica i, odpowiednio, przeliczać się suma jej elementów.

Zbierzmy cały nasz kod razem:

jsxfunction App() {
  const [notes, setNotes] = useState([1, 2, 3]);
  
  function changeHandler(index, event) {
    setNotes([...notes.slice(0, index), event.target.value, ...notes.slice(index + 1)]);
  }
  
  return <div>
    <input value={notes[0]} onChange={event => changeHandler(0, event)} />
    <input value={notes[1]} onChange={event => changeHandler(1, event)} />
    <input value={notes[2]} onChange={event => changeHandler(2, event)} />
    
    {getSum(notes)}
  </div>;
}

Można zrobić tak, żeby inputy tworzyły się w pętli:

jsxfunction App() {
  const [notes, setNotes] = useState([1, 2, 3]);
  
  function changeHandler(index, event) {
    setNotes([...notes.slice(0, index), event.target.value, ...notes.slice(index + 1)]);
  }
  
  const result = notes.map((note, index) => {
    return <input
      key={index}
      value={note}
      onChange={event => changeHandler(index, event)}
    />;
  });
  
  return <div>
    {result}
    {getSum(notes)}
  </div>;
}
Задание 1

Dana jest następująca tablica:

jsx[1, 2, 3, 4, 5, 6, 7, 8, 9]

Wypisz na ekran średnią arytmetyczną elementów tej tablicy. W pętli zrób inputy do edycji elementów.