Skip to content

Instantly share code, notes, and snippets.

@francodalessio
Created April 1, 2020 03:00
Show Gist options
  • Save francodalessio/9e5128521b4177dfa5589761524c64fe to your computer and use it in GitHub Desktop.
Save francodalessio/9e5128521b4177dfa5589761524c64fe to your computer and use it in GitHub Desktop.
import React, { useState } from "react";
function useArray(initialList) {
const [list, setList] = useState(initialList);
return {
list,
addItem: newItemText => {
setList([
...list,
{
id: list.length + 1,
text: newItemText,
completed: false
}
]);
},
removeItem: itemId => {
const updatedItems = list.filter(item => item.id !== itemId);
setList(updatedItems);
},
toggleItem: itemId => {
const updatedItems = list.map(item => {
return item.id === itemId
? { ...item, completed: !item.completed }
: item;
});
setList(updatedItems);
}
};
}
export default useArray;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment