Created
April 1, 2020 03:00
-
-
Save francodalessio/9e5128521b4177dfa5589761524c64fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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