这是我的副本:https://repl.it/@colegonzales1/HeftyJoyfulAbilities我知道如何通过我的应用程序传递状态并创建一个为每个项目设置样式的处理程序,但是我觉得有一种更简单的方法。我试图找到一种方法来基于其状态值来设置项目样式。我知道.todo-item [done ='true'],但是由于我的状态是按原样嵌套的,所以它不起作用。我的CSS专家有什么想法吗?哈哈
还有保存状态的Main.js
import React, { Component } from 'react';
import TodoInput from './todo-input';
import TodoList from './TodoList';
class App extends Component {
constructor(props){
super(props);
this.state = {
todos: [],
inputValue: ''
}
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleRemove = this.handleRemove.bind(this);
this.handleToggle = this.handleToggle.bind(this);
};
handleChange = (e) => {
e.preventDefault();
this.setState({
inputValue: e.target.value
});
}
handleSubmit = (e) => {
e.preventDefault();
const newTodo = {
title: this.state.inputValue,
id: Date.now(),
done: false
};
this.setState((prevState) => ({
todos: [...prevState.todos, newTodo],
inputValue: ''
}));
}
handleRemove (e, id) {
e.preventDefault();
const newTodos = this.state.todos.filter(todo => todo.id !== parseInt(e.target.id));
this.setState({
todos: newTodos
});
console.log(newTodos);
}
handleToggle (e) {
const id = parseInt(e.target.id);
this.setState((prevState) => ({
todos: prevState.todos.map(todo => todo.id === id ? {...todo, done: !todo.done} : todo)
}));
console.log(id);
}
render() {
return (
<div>
<TodoInput
value={this.state.inputValue}
onChange={this.handleChange}
onSubmit={this.handleSubmit}
/>
<TodoList
todosArr={this.state.todos}
onRemove={this.handleRemove}
onToggle={this.handleToggle}
/>
</div>
);
}
}
export default App;