Skip to content

Instantly share code, notes, and snippets.

@shanebdavis
Created December 28, 2019 21:07
Show Gist options
  • Save shanebdavis/29774871b282797b45f74a9f347ccf6d to your computer and use it in GitHub Desktop.
Save shanebdavis/29774871b282797b45f74a9f347ccf6d to your computer and use it in GitHub Desktop.
modular-redux-tutorial component ToDo
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