Can someone explain why this event is fired off twice?
Here is my mainContent Component
class MainContent extends React.Component {
constructor() {
super()
this.state = {
todos: ToDosData
}
this.handleChange = this.handleChange.bind(this)
}
handleChange(id) {
this.setState(prevState => {
const updatedToDos = prevState.todos.map(todo => {
if (todo.id === id) {
console.log(!todo.completed)
todo.completed = !todo.completed
}
return todo
})
console.log(updatedToDos)
return {
todos: updatedToDos
}
})
}
render() {
const mainBodyStyles = {
color: "#FF8C00",
backgroundColor: "#fG7B02",
}
const todoItems = this.state.todos.map(item =>
<TodoItem
key={item.id}
item={item}
handleChange={this.handleChange}
/>)
return (
<div style={mainBodyStyles}>
{todoItems}
</div>
)
}
here is my toDo component
function TodoItem(props) {
return (
<div>
<input
type="checkbox"
checked={props.item.completed}
onChange={() => props.handleChange(props.item.id)}
/>
<p>{props.item.text}</p>
</div>
)
}
When I click on a checkbox It runs the event function twice. I can't wrap my head around what I am doing wrong. Thanks in advance.