Skip to content

Instantly share code, notes, and snippets.

@pubudu-ranasinghe
Last active November 2, 2019 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pubudu-ranasinghe/48aefaca21ca648f3ff3da448977fe69 to your computer and use it in GitHub Desktop.
Save pubudu-ranasinghe/48aefaca21ca648f3ff3da448977fe69 to your computer and use it in GitHub Desktop.
react-context-example-04
import React, { useState } from "react";
import { ItemList, NewItem } from "./components/Items";
import "./App.css";
const initialItems = ["Setup basic components", "Add some styling"];
function App() {
// useState hook returns two values. First is the state itself
// and second is a function that we can use to update the state
const [items, setItems] = useState(initialItems);
function handleAddItem(item) {
setItems([...items, item]);
}
function handleRemoveItem(index) {
const copy = [...items];
copy.splice(index, 1);
setItems(copy);
}
return (
<div className="App">
<header className="App-header">
<h2>🚀 ToDo App</h2>
<NewItem add={handleAddItem} />
<ItemList items={items} remove={handleRemoveItem} />
</header>
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment