Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fix error "Argument of type 'void[]' is not assignable to parameter of type 'SetStateAction<ITask[]>'" in typescript react

I am trying to build a todo list app, and I am new to typescript. When the complete button is pressed on a task, I want it to be marked as completed. When I press the button the data has changed, but what is shown on the screen hasn’t changed. Here is the part in the code that is messing up:

const completeTask = (id: number) => {

    let tasks = todos.map((task) =>  {
      if (task.id === id) {
        task.completed = true
        console.log(todos)
      }
    })

    setTodos(tasks)

  }

Here is the full code:

import React, { FC, useState } from 'react';
import {TodoItem} from './components/TodoItem';

interface ITask {
  id: number;
  todo: string;
  startDate: string;
  endDate?: string;
  completed: boolean;
}

const App: FC = () => {
  
  const [todos, setTodos] = useState<ITask[]>([
    {
    id: 0,
    todo: "Buy Bread",
    startDate: "12/12/2023",
    endDate: "12/14/2023",
    completed: false
  }, {
    id: 1,
    todo: "Buy House",
    startDate: "12/12/2023",
    endDate: "12/13/2023",
    completed: false
    }
  ])
  
  const completeTask = (id: number) => {

    let tasks = todos.map((task) =>  {
      if (task.id === id) {
        task.completed = true
        console.log(todos)
      }
    })

    setTodos(tasks)

  }

  return (
    <>
      {todos.map((task, key) => (
        <TodoItem id={key} todo={task.todo} completed={task.completed} completeItem={() => completeTask(key)} startDate={task.startDate} endDate={task.endDate === undefined ? "" : task.endDate} />
      ))}
    </>
  );
}

export default App;

Here is the code for the TodoItem:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import React, { FC } from 'react';
// import arrow from '../public/arrow.svg';

export interface ITodoItemProps {
  id: number;
  todo: string;
  startDate: string;
  endDate?: string;
  completed: boolean;
  completeItem: (id: number) => void;
}

export const TodoItem: FC<ITodoItemProps> = ({id, todo, startDate, endDate, completeItem, completed}) => {
  return (
    <div className={`bg-white rounded-[10px] flex flex-col bg-white w-[300px] p-[18px] h-[270px] justify-between m-10 shadow-[2px_4px_20px_0px_rgba(0, 0, 0, 0.04)] ${completed === true ? 'border-[rgba(36, 46, 61, 0)] border-2' : ''}`}>
      <div>
        <div className='flex flex-row gap-3 mb-4 align-middle'>
          <img src="images/calendar.svg" className='w-[20px]' alt="" />
          <p className='pb-0 mb-0 font-mono text-[12px] align-middle text-third mt-1'>{startDate}</p>
          <img src="images/arrow.svg" alt="" />
          <p className='pb-0 mb-0 font-mono text-[12px] align-middle text-third mt-1'>{endDate}</p>
        </div>
        <p className='text-[17px] leading-[25px] max-h-[150px] overflow-y-auto'>{todo}</p>
      </div>
      <div className='w-full py-[8px] px-[12px] bg-second rounded-[10px] flex flex-row justify-between'>
        <img src="images/edit.svg" alt="" className='hover:cursor-pointer' />
        <div className='flex flex-row'>
          <button className='text-third px-[12px]'>Delete</button>
          <button onClick={() => completeItem(id)} className="bg-white px-[12px] py-[4px] rounded-[10px]" >Complete</button>
        </div>
      </div>
    </div>
  );
}

Here is the error:

TS2345: Argument of type 'void[]' is not assignable to parameter of type 'SetStateAction<ITask[]>'.
  Type 'void[]' is not assignable to type 'ITask[]'.
    Type 'void' is not assignable to type 'ITask'.
    37 |     })
    38 |
  > 39 |     setTodos(tasks)
       |              ^^^^^
    40 |
    41 |   }
    42 |

>Solution :

You forgeted return item in the map callback. I hope it’s helpful for you.

  const completeTask = (id: number) => {
    let tasks = todos.map((task) => {
      if (task.id === id) {
        task.completed = true;
        console.log(todos);
      }
      // you forgeted return value
      return task
    });

    setTodos(tasks);
  };
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading