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 DisplayTodos = props => { | |
return ( | |
<AppConsumer> | |
{({ todos, completed }) => { | |
const notCompleted = todos.filter(todo => { | |
if (todo.completed === false) { | |
return 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
const TodoInput = props => { | |
let todo; | |
return ( | |
<AppConsumer> | |
{({ addTodo }) => ( | |
<form | |
onSubmit={e => { | |
e.preventDefault(); | |
}} | |
> |
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
class TodoApp extends Component { | |
render() { | |
return ( | |
<div className="todos"> | |
<Grid columns={1} className="ui center aligned"> | |
<Grid.Row> | |
<Grid.Column className="todo-body" width={12}> | |
<h2>Simple Todo</h2> | |
<TodoInput /> | |
<div id="display-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
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 |
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 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
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
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
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
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); |
NewerOlder