简体   繁体   English

我正在尝试使用迭代语句更新 React 组件中的类名,但由于某种原因未更新类名。 怎么来的?

[英]I'm trying to update a className in a React component, using itenerary statement but for some reason the className is not updated. How come?

This is a simple todo list app.这是一个简单的待办事项列表应用程序。 I'm using a ternary condition in order to update a child component (third line).我正在使用三元条件来更新子组件(第三行)。 once the user clicks on the second button (last button), a function in the parent component will be called (passed as a prop property) and update that the task is completed by updating the isCompleted bolead (another click will reverse the decision).一旦用户点击第二个按钮(最后一个按钮),父组件中的 function 将被调用(作为 prop 属性传递)并通过更新 isCompleted bolead 更新任务完成(再次点击将撤销决定)。 However, although the object is being updated with the property, the className will not update.然而,虽然 object 正在更新属性,但 className 不会更新。 What could be the problem and/or solution?问题和/或解决方案可能是什么? Could it be because the child component is not being rerendered after the update?会不会是更新后子组件没有重新渲染? thanks in advance.提前致谢。 This is the part being returned from the child component:这是从子组件返回的部分:

GoalsList.map((item:IGoal)=>{
                          return  <div className='goal-content' key={item.id}>
                            <span className={item.isCompleted ?  'linethrough-true':''}>{`${item.isCompleted}  ${item.goal} to be completed in ${item.deadline} days.`} </span>
                            <span className='buttons-container'>
                            <button onClick={()=>{ 
                                deleteGoal(item.id);
                              }}> <TfiTrash className='icon icon-trash'/></button>
                              <button onClick={()=>{ 
                                completeGoal(item.id);}}><TfiCheck className='icon icon-check'/></button>
                            </span>
                          
                            </div>
                     })

This is the function handling the click at the parent component:这是处理父组件点击的 function:

 const completeGoal=(goalCompleted:string):void=>{
        let newGoalsList=GoalsList; 
        newGoalsList.forEach((goal)=>{
        if (goalCompleted==goal.id)
          {
            if (goal.isCompleted==false)
              goal.isCompleted=true;
            else
              goal.isCompleted=false;
          }
          
      }
        );

        setGoalsList(newGoalsList);
        
  }

I wish that the goal property which is shown at the span which contains the className, will be affected by the className update and show a line through each time the isCompleted property is updated to be true.我希望在包含 className 的范围内显示的目标属性将受到 className 更新的影响,并在每次 isCompleted 属性更新为 true 时显示一条线。

First of all you are mutating the react state.首先,你正在改变反应 state。

 let newGoalsList=GoalsList;

so avoid that first.所以先避免这种情况。 the best way to avoid that is avoiding use of forEach and instead replace with map and add the logic while iterating just make sure you return a new goal after updating the isCompleted field.避免这种情况的最佳方法是避免使用 forEach,而是替换为 map 并在迭代时添加逻辑,只需确保在更新 isCompleted 字段后返回新目标即可。 some something like this.一些像这样的东西。

 const newGoalsList = GoalsList.map((goal) => {
   if(goal.id === goalCompleted){
     return {...goal, isCompleted: !goal.isCompleted}
   } else {
      return {...goal}
   }
});

Now you can set your set your state.现在您可以设置您的 state。

setGoalsList(newGoalsList);设置目标列表(新目标列表);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM