Skip to content

Instantly share code, notes, and snippets.

@pubudu-ranasinghe
Last active November 3, 2019 10:12
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/66c0b15127584d8f0326b7955159b5e3 to your computer and use it in GitHub Desktop.
Save pubudu-ranasinghe/66c0b15127584d8f0326b7955159b5e3 to your computer and use it in GitHub Desktop.
react-context-example-08
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