Skip to content

Instantly share code, notes, and snippets.

@DennisDurairaj
Created May 9, 2020 14:45
Show Gist options
  • Save DennisDurairaj/3dc0688ba9d645c9e80c4133b14544cf to your computer and use it in GitHub Desktop.
Save DennisDurairaj/3dc0688ba9d645c9e80c4133b14544cf to your computer and use it in GitHub Desktop.
import React, { useState } from "react";
import "./App.css";
function App() {
const [items, setItems] = useState([]);
const itemList = [ { id: 1, value: "London" },
{ id: 2, value: "Paris" },
{ id: 3, value: "Tokyo" },
{ id: 4, value: "Berlin" } ];
const addItem = () => {
const itemToAdd = itemList.find(item => {
const checkExists = items.filter(stateItem => { return stateItem.id === item.id; });
if (checkExists.length === 0) {
return item;
}
});
setItems([...items, itemToAdd]);
};
const removeItem = item => {
const newItems = items.filter(stateItem => stateItem.id !== item.id);
setItems(newItems);
};
return (
<div className="App">
<button disabled={items.length === itemList.length} onClick={addItem}> Add item </button>
{items.length > 0 && items.map(item => (
<div>
<button onClick={() => removeItem(item)}>remove</button>{" "}
{item.value}
<input />
</div> ))}
</div> );
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment