React တွင် Input များကို Array နှင့် ချိတ်ဆက်ခြင်း

State ထဲတွင် notes array တစ်ခု သိမ်းထားသည်ဆိုပါစို့။

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

ထို့အပြင် Array ၏ element များစုစုပေါင်းကို ရှာပေးသည့် အကူ Function တစ်ခုလည်း ရှိသည်ဆိုပါစို့။

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

function App() {
  ...
}

ကျွန်ုပ်တို့၏ အကူ Function ကို အသုံးပြု၍ State မှ Array ၏ element များစုစုပေါင်းကို ရှာဖွေပြီး ပြသကြပါစို့။

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

ယခု Input သုံးခု ဖန်တီးကြပါစို့။ ၎င်းတို့တစ်ခုစီ၏ value တွင် Array ၏ element တစ်ခုစီကို ထည့်သွင်းထားပါမည်။

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

ယခု ကျွန်ုပ်တို့၏ Input များအတွက် onChange event ကို ထည့်သွင်းကြပါစို့။ ဤသို့ပြုလုပ်ရာတွင် Event အတွက် အထွေထွေ Function တစ်ခုတည်းကို Handler အဖြစ် အသုံးပြုပါမည်။

jsxfunction App() {
  const [notes, setNotes] = useState([1, 2, 3]);
  
  function changeHandler(index, event) {
    // အထွေထွေ function-handler
  }
  
  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>;
}

မြင်တွေ့ရသည့်အတိုင်း၊ changeHandler function သည် ပထမ Parameter အဖြစ် ၎င်း Input က Array ၏ မည်သည့် Element ကို တည်းဖြတ်သည်ဆိုသော အမှတ်စဉ်ကို လက်ခံပါသည်။

ထို အမှတ်စဉ်ကို အသုံးပြု၍ Array element ကို Input ၏ အကြောင်းအရာဖြင့် အစားထိုးနိုင်ပါသည်။

ဤသို့ပြုလုပ်ကြပါစို့။

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

ယခုဆိုလျှင် မည်သည့် Input ကိုမဆို တည်းဖြတ်နိုင်ပြီး၊ ၎င်းနှင့်အတူ Array သည် Reactively ပြောင်းလဲသွားမည်ဖြစ်ကာ ထို့ကြောင့် ၎င်း၏ Element များ၏ စုစုပေါင်းကို ပြန်လည်တွက်ချက်မည်ဖြစ်သည်။

ကျွန်ုပ်တို့၏ ကုဒ်အားလုံးကို အတူတကွ စုစည်းကြပါစို့။

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

Input များကို Loop ဖြင့် ဖွဲ့စည်းနိုင်ပါသည်။

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

အောက်ပါ Array ကို ပေးထားသည်။

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

ထို Array ၏ element များ၏ ပျမ်းမျှတန်ဖိုး (Arithmetic Mean) ကို စခရင်ပေါ်တွင် ပြသပါ။ Element များကို တည်းဖြတ်ရန် Loop အတွင်း Input များ ပြုလုပ်ပါ။