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
<AppContext.Provider value={{name: 'Ben Kissi'}}> | |
{/* Put child components here */} | |
</AppContext.Provider> |
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
<AppContext.Consumer> | |
{({name})=> (<p>Hello {name}</p>)} | |
</AppContext.Consumer> |
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, { Component } from "react"; | |
const AppContext = React.createContext(); | |
class AppProvider extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
todos: [] | |
}; | |
this.addTodo = this.addTodo.bind(this); | |
this.completed = this.completed.bind(this); |
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
addTodo(todo) { | |
const { todos } = this.state; | |
const newTodo = { | |
id: Math.floor(Math.random() * (1000 - 1)) + 1, | |
todo: todo, | |
createdAt: Date.now(), | |
completed: false | |
}; | |
this.setState({ | |
todos: todos.concat(newTodo) |
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
completed(id) { | |
const { todos } = this.state; | |
todos.forEach(todo => { | |
if (todo.id === id) { | |
todo.completed = !todo.completed; | |
} | |
}); | |
this.setState({ | |
todos: todos | |
}); |
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
render() { | |
return ( | |
<AppContext.Provider | |
value={{ | |
todos: this.state.todos, | |
addTodo: this.addTodo, | |
completed: this.completed | |
}} | |
> | |
{this.props.children} |
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
const AppConsumer = AppContext.Consumer; | |
export { AppProvider, AppConsumer }; |
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
const rootElement = document.getElementById("root"); | |
ReactDOM.render( | |
<AppProvider> | |
<TodoApp /> | |
</AppProvider>, | |
rootElement | |
); |
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
const AppContext = React.createContext('hello world'); |
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, { Component } from "react"; | |
import { AppConsumer } from "./app-context"; | |
import { Grid, Button, Input } from "semantic-ui-react"; | |
const TodoInput = props => { | |
let todo; | |
return ( | |
<AppConsumer> | |
{({ addTodo }) => ( | |
<form |
OlderNewer