From 9e94ff921cb476d5f08dfbdf3f954262127eed8a Mon Sep 17 00:00:00 2001 From: Olha Danylevska Date: Sat, 1 Jul 2023 15:44:58 +0100 Subject: [PATCH] delete names from favorit all names sorted in main listafter removed from favorite --- src/App.js | 3 ++- src/display-the-names.js | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/App.js b/src/App.js index de6f15c..ef62f4c 100644 --- a/src/App.js +++ b/src/App.js @@ -9,8 +9,9 @@ function App() { const sortedNames = (names, key) => { return names.sort((a, b) => a[key].localeCompare(b[key])); }; - const [searchInput, setSearchInput] = useState(""); + const initialData = sortedNames(babyNamesData, "name"); + const [searchInput, setSearchInput] = useState(""); const [mainList, setMainList] = useState(initialData) return ( diff --git a/src/display-the-names.js b/src/display-the-names.js index cc8e05b..2547e90 100644 --- a/src/display-the-names.js +++ b/src/display-the-names.js @@ -13,7 +13,13 @@ const DisplayNames = ({ allNames, setMainList }) => { const handleDelete = (name) => { setFavoriteName(favoriteName.filter((babyName) => babyName.name !== name.name)) - setMainList(allNames.concat(name)) + let sortedNamesAfterFavorite = (names, key) => { + return names.sort((a, b) => a[key].localeCompare(b[key])); + }; + + const allNamesAfterFavorite = sortedNamesAfterFavorite([...allNames, name], "name"); + setMainList(allNamesAfterFavorite) + } console.log({ favoriteName })