eGroupTeam / course-frontend

0 stars 2 forks source link

關於useState #50

Open ginnypinjyunlin opened 1 year ago

ginnypinjyunlin commented 1 year ago

請問一個元件有可能有多個state嗎? 這樣要如何使用useState呢?

jitsungwu commented 1 year ago

可以,在ProductList.tsx裡就用到多個useState()。

  const [products, setProducts]=useState<Product[]>([]) 
  const [open, setOpen] = useState(false);
  const [deleted, setDeleted] = useState(false);
ginnypinjyunlin commented 1 year ago

查找的文章https://ithelp.ithome.com.tw/articles/10277466?sc=rss.iron

useState設定的初始值不限型別,可以是 string、number、array、object 或 callBack Function(必須要有回傳值)。因此當需要多個變數時,可依照需求呼叫多個 useState