Thuta Learning
ရှာဖွေရန်
BasicWeb Developmentintermediate

Lists & Keys

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

React မှာ array data ကို UI list အဖြစ်ပြဖို့ JavaScript ရဲ့ `map()` ကိုအသုံးပြုပါတယ်။ Blog posts, products, menu items, notifications, lessons စတဲ့ repeated UI တွေကို list rendering နဲ့တည်ဆောက်နိုင်ပါတယ်။

jsx
function TodoList() {
  const todos = [
    { id: 1, text: 'Learn React' },
    { id: 2, text: 'Build a project' },
    { id: 3, text: 'Deploy the project' }
  ];

  return (
    <ul>
      {todos.map(todo => (
        <li key={todo.id}>{todo.text}</li>
      ))}
    </ul>
  );
}

`todos.map()` က array ထဲက item တစ်ခုချင်းစီကို `

` element အဖြစ်ပြောင်းပေးပါတယ်။ `key={todo.id}` က React ကို item တစ်ခုချင်းစီကိုခွဲခြားသိစေပါတယ်။

You should see
Bullet list သုံးကြောင်း—Learn React, Build a project, Deploy the project—ပေါ်လာမယ်။

Info

List ထဲမှာ item add/remove/reorder လုပ်တဲ့အခါ key မှန်မှ UI update ကခန့်မှန်းရလွယ်ပြီး bug နည်းပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • `key` မထည့်ရင် console warning ပေါ်ပြီး list update အချို့မှာ UI မတည်ငြိမ်နိုင်ပါတယ်။
Lists & Keys | Thuta Learning