Created
December 28, 2019 21:07
-
-
Save shanebdavis/29774871b282797b45f74a9f347ccf6d to your computer and use it in GitHub Desktop.
modular-redux-tutorial component ToDo
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"; | |
import { ToDoItem } from "./ToDoItem"; | |
import { addItem, useList } from "../redux/list"; | |
export const ToDo = () => { | |
const list = useList(); | |
const [text, setText] = useState(""); | |
const createNewToDoItem = () => { | |
if (!text) return alert("Please enter text!"); | |
addItem({ text }); | |
setText(""); | |
}; | |
return ( | |
<div> | |
<ul> | |
{list.map(item => ( | |
<ToDoItem key={item.id} item={item} /> | |
))} | |
</ul> | |
<input | |
type="text" | |
value={text} | |
onChange={e => setText(e.target.value)} | |
onKeyPress={e => e.key === "Enter" && createNewToDoItem()} | |
/> | |
<button onClick={createNewToDoItem}>{" + "}</button> | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment