Skip to content

'HW-0810' #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@ import TodoList from './TodoList.jsx'
import TodoAddForm from './TodoAddForm.jsx'

class TodoApp extends React.Component {
state = {
todos:[]
}
addToDo = (go) =>{
this.setState({todos:this.state.todos.concat(go)});
console.log(this.state);
}
deleteToDo = (away) =>{
this.state.todos.splice(away,1);
this.setState({todos:this.state.todos});
}
render() {
return (
<div>
<h2>Todo App</h2>
123
<h2>Todo App之我不會只能參考助教的</h2>
<TodoAddForm addToDo={this.addToDo}/>
<TodoList todos={this.state.todos} deleteToDo={this.deleteToDo}/>
</div>
);
}
Expand Down
19 changes: 14 additions & 5 deletions src/TodoAddForm.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import React from 'react'

class TodoAddForm extends React.Component {
state = {
inputText: ''
constructor(props){
super(props)
this.state = {
inputText: ''
};
}
handleChange = (E) => {
this.setState({inputText: E.target.value});
}
addToDo = (E) =>{
this.props.addToDo(this.state.inputText);
this.setState({inputText:''});
}

render() {
return (
<div>
<input type="text" value={this.state.inputText}/>
<button>新增</button>
<input type="text" value={this.state.inputText} onChange = {this.handleChange}/>
<button onClick = {this.addToDo}>新增</button>
</div>
);
}
Expand Down
8 changes: 7 additions & 1 deletion src/TodoItem.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import React from 'react'

class TodoItem extends React.Component {
constructor(props){
super(props)
}
deleteToDo = ()=> {
this.props.deleteToDo(this.props.position);
};
render() {
return (
<div>

<span>{this.props.value}</span><button onClick={this.deleteToDo}>刪除</button>
</div>
);
}
Expand Down
9 changes: 8 additions & 1 deletion src/TodoList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ import React from 'react'
import TodoItem from './TodoItem.jsx'

class TodoList extends React.Component {
constructor(props){
super(props)
}
render() {
return (
<div>

{this.props.todos.map((todo,i)=>{
return(
<TodoItem value={todo} deleteToDo={this.props.deleteToDo} />
);
})}
</div>
);
}
Expand Down