我正在用 React 做一个简单的待办事项应用程序,只是为了练习。单击列表项时,如何删除它?
这是我的 todos.js
export default class Todos extends Component {
constructor(props) {
super(props);
this.state = { todos: [], text: '' };
}
addTodo(e) {
e.preventDefault();
this.setState({ todos: [ this.state.text, ...this.state.todos ] });
this.setState({ text: ''});
}
updateValue(e) {
this.setState({ text: [e.target.value]})
}
render() {
return(
<div>
<form onSubmit = {(e) => this.addTodo(e)}>
<input
placeholder="Add Todo"
value={this.state.text}
onChange={(e) => {this.updateValue(e)}}
/>
<button type="submit">Add Todo</button>
</form>
<TodoList todos={this.state.todos}/>
</div>
);
}
}
这是 TodoList.js,我试图从中删除列表项。
import React, { Component } from 'react';
import { connect } from 'react-redux';
export default class TodoList extends Component {
removeItem(e) {
// splice this.props.todos??
}
render() {
return(
<ul>
{ this.props.todos.map((todo) => {
return <li onClick={(e) => { this.removeItem(e)}} key={todo}>{ todo }</li>
})}
</ul>
);
}
}