Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
how to keep the last component styles?

i'm building a to-do app using React Js . inside the task component i used a state to apply a certain styles for the completed task and it works fine . but , after i cliked any delete button the style of the completed task deleted . how can i prevent that ?

import Task from "../Task/Task";
import style from "./TasksList.module.css";

const TasksList = ({ tasks, deleteTaskHandler }) => {
  return (
    <div className={style.tasks}>
      <div className="container">
        {tasks.map((task, idx) => {
          return (
            <Task
              task={task}
              id={idx}
              key={Math.random()}
              deleteTaskHandler={deleteTaskHandler}
            />
          );
        })}
      </div>
    </div>
  );
};

export default TasksList;
import { useState } from "react";
import style from "./Task.module.css";

const Task = ({ task, id, deleteTaskHandler }) => {
  const [isComplete, setIsComplete] = useState(false);

  const markComplete = () => {
    setIsComplete(!isComplete);
  };

  return (
    <div
      className={
        isComplete ? `${style.task}  ${style.completed}` : `${style.task}`
      }
      onClick={markComplete}
    >
      <label>{task.desc}</label>
      <button onClick={() => deleteTaskHandler(id)}> Delete </button>
    </div>
  );
};

export default Task;
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

How are you maintaining the complete status of task in higher components?

Currently you are not initializing the complete state of Task. If the task object contains the isComplete property, then you can use as shown below

 const [isComplete, setIsComplete] = useState(task.isComplete);

however, you also need to update value of completed in task. So, I would suggest to have all lower components as stateless. and maintain the state at Higher component i.e. TaskList

import style from "./Task.module.css";

const Task = ({ task, id, deleteTaskHandler, setTaskCompleteHandler }) => {

  const markComplete = () => {
    setTaskCompleteHandler(!task.isComplete);
  };

  return (
    <div
      className={
        task.isComplete ? `${style.task}  ${style.completed}` : `${style.task}`
      }
      onClick={markComplete}
    >
      <label>{task.desc}</label>
      <button onClick={() => deleteTaskHandler(id)}> Delete </button>
    </div>
  );
};

export default Task;

Implement setTaskCompleteHandler in TaskList and pass is as prop as part of Task component reandering.

about 4 years ago · Juan Pablo Isaza Report

0

Your problem is in position of your delete button, that wrapped by div with makrComplete handler, so then you click on your delete button, markComplete fired too, so your isComplete changed and styles deleted. To prevent this behavor, you can do a little trick with prevent default. So, try to wrap your deleteTaskHandler in another function like that:

const deleteButtonClickHandler = (e) => {
    e.preventDefault();
    e.stopPropagation();

    deleteTaskHandler(id)
}

and your delete button makrdown should be look like this:

<button onClick={deleteButtonClickHandler}> Delete </button>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!