Skip to content

Instantly share code, notes, and snippets.

@achukka
Created May 13, 2021 04:07
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 achukka/94ca905b81c052d10cff90de735d28ff to your computer and use it in GitHub Desktop.
Save achukka/94ca905b81c052d10cff90de735d28ff to your computer and use it in GitHub Desktop.
Gist for App with AddItem and Display
import React from "react";
import AddItem from "./AddItem";
import ToDoList, { Item } from "./ToDoList";
const initialList = [
{
task: "Pick up Milk",
priority: 1,
},
{
task: "Buy Eggs",
priority: 2,
},
{
task: "Buy Bread",
priority: 3,
},
];
const isPartOf = (item: Item, items: Item[]): boolean => {
return items.some((it) => it.priority === item.priority);
};
class App extends React.Component<{}, { items: Item[] }> {
constructor(props: any) {
super(props);
this.state = {
items: initialList,
};
this.addItem = this.addItem.bind(this);
}
addItem(item: Item) {
const { items } = this.state;
if (isPartOf(item, items)) {
alert(`Item with priorirty: ${item.priority} exists`);
return;
}
this.setState({
items: items.concat(item),
});
}
render() {
const { items } = this.state;
return (
<div className="App">
<AddItem addItem={this.addItem} />
<br />
<ToDoList items={items} />
</div>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment