Created
March 9, 2019 05:34
-
-
Save asleepysamurai/b8713d8f82b4570f9448cce73acae9c3 to your computer and use it in GitHub Desktop.
TodoItem Class Component
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
/** | |
* TodoItem Component | |
*/ | |
import React, { Component } from 'react'; | |
class TodoItem extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
completed: false | |
}; | |
} | |
toggleItemCompleted = () => { | |
this.props.toggleItemCompleted(this.props.id); | |
} | |
static getDerivedStateFromProps(props, state) { | |
const todoItemIndexInCompletedItemIds = props.completedItemIds.indexOf(props.id); | |
return { completed: todoItemIndexInCompletedItemIds > -1 }; | |
} | |
render() { | |
return ( | |
<div | |
className="todo-item"> | |
<input | |
id={`completed-${this.props.id}`} | |
type="checkbox" | |
onChange={this.toggleItemCompleted} | |
checked={this.state.completed} /> | |
<label>{this.props.text}</label> | |
</div> | |
); | |
} | |
}; | |
export default TodoItem; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment