【问题标题】:React, updating array to a new array doesn't re-render the dom with the new arrayReact, updating array to a new array doesn\'t re-render the dom with the new array
【发布时间】:2022-12-27 21:57:09
【问题描述】:

so i'm looping over an array, and every element has a click event :

{optionsCategory.map((c,i) => (
     <div className="content" key={i} onClick={(e) => handleCategoryClick(e,c)}>
        {c.name}
     </div>
     ))}

upon clicking if the element has a subCategory i want to assing the subCategory array to the optionsCategory

const handleCategoryClick = async (e: React.MouseEvent<HTMLHeadingElement>, c: Category) => {
if(c.subCategories.length > 0) {
  setOptionsService([...c.subCategories])
  console.log([optionsCategory])
}
else{
  setIsCategory(true);
  const data = await onActsByServiceIdAndCategoryId(serviceId,c.id);
  setActs([...data])
  console.log(acts);
}}

in the console the optionsCategory is updated but not in the Dom

【问题讨论】:

    标签: javascript reactjs typescript


    【解决方案1】:

    Are you using useState hook for optionsCategory ?

    【讨论】:

      猜你喜欢
      • 2022-12-26
      • 2022-12-01
      • 2022-12-02
      • 2022-12-27
      • 1970-01-01
      • 2018-11-07
      • 2022-12-02
      • 2022-12-01
      • 1970-01-01
      相关资源
      最近更新 更多