Last active
November 3, 2019 10:12
-
-
Save pubudu-ranasinghe/66c0b15127584d8f0326b7955159b5e3 to your computer and use it in GitHub Desktop.
react-context-example-08
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, useContext } from "react"; | |
import TodoContext from "../contexts/TodoContext"; | |
export function NewItem() { | |
const [text, setText] = useState(""); | |
// TodoContext is made available as a hook | |
const todoContext = useContext(TodoContext); | |
return ( | |
<div className="Item"> | |
<input | |
type="text" | |
placeholder="New Task" | |
value={text} | |
onChange={e => setText(e.target.value)} | |
></input> | |
<button onClick={() => todoContext.add(text)}>Add</button> | |
</div> | |
); | |
} | |
export function ItemList() { | |
const todoContext = useContext(TodoContext); | |
return todoContext.items.map((item, i) => ( | |
<Item text={item} index={i} key={i} remove={todoContext.remove} /> | |
)); | |
} | |
// Rest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment