Skip to content

Instantly share code, notes, and snippets.

@asleepysamurai
Created March 9, 2019 05:34
Show Gist options
  • Save asleepysamurai/b8713d8f82b4570f9448cce73acae9c3 to your computer and use it in GitHub Desktop.
Save asleepysamurai/b8713d8f82b4570f9448cce73acae9c3 to your computer and use it in GitHub Desktop.
TodoItem Class Component
/**
* 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