Propojení inputů s polem v Reactu

Nechť ve stavu notes je uloženo pole:

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

Nechť máme také pomocnou funkci, která vypočítá součet prvků pole:

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

function App() {
  ...
}

Pojďme najít a zobrazit součet prvků našeho pole ze stavu, přičemž použijeme naši pomocnou funkci:

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

Nyní vytvořme tři inputy a do value každého inputu vložme jeden z prvků pole:

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>;
}

Nyní přidejme událost onChange naším inputům. Přitom vytvořme jednu společnou funkci-obsluhu této události:

jsxfunction App() {
  const [notes, setNotes] = useState([1, 2, 3]);
  
  function changeHandler(index, event) {
    // společná funkce-obsluha
  }
  
  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 vidíte, funkce changeHandler jako první parametr přijímá číslo toho prvku pole, který daný input upravuje.

Podle tohoto čísla můžeme nahradit prvek pole obsahem inputu.

Pojďme to udělat:

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

Nyní bude možné upravit libovolný input, přičemž reaktivně se změní pole a podle toho se přepočítá součet jeho prvků.

Pojďme sloučit celý náš kód dohromady:

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>;
}

Lze to udělat tak, aby se inputy vytvářely v cyklu:

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

Je dáno následující pole:

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

Zobrazte na obrazovce aritmetický průměr prvků tohoto pole. V cyklu vytvořte inputy pro úpravu prvků.