Skip to content

2017-hellojs_overflow-kaya #3

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
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
31 changes: 29 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
import React from 'react'
import TodoList from './TodoList.jsx'
import TodoAddForm from './TodoAddForm.jsx'
// import List from './List.jsx'

class TodoApp extends React.Component {
constructor(props) {
super(props)
this.state = {
todos: []
}
}

addTodo(todo) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以試著用 arrow function 減少寫 bind this的麻煩

let update_todos = this.state.todos.slice(0)
update_todos.push(todo)
// console.log('update_todos', update_todos)
this.setState({
todos: update_todos
})
}

deleteTodo(idx) {
let update_todos = this.state.todos.slice(0)
update_todos.splice(idx, 1)
this.setState({
todos: update_todos
})
}

render() {
return (
<div>
<h2>Todo App</h2>
123
<TodoAddForm addTodo={this.addTodo.bind(this)} />
<TodoList todos={this.state.todos} deleteTodo={this.deleteTodo.bind(this)}/>
</div>
);
)

}
}

30 changes: 30 additions & 0 deletions src/List.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react'

class List extends React.Component {
constructor(props) {
super(props)
this.state = {
input_text: '',
}

}

onTextChange(evt) {
this.setState({
input_text: evt.target.value
})
}

render() {
const items = ['a', 'b', 'c', 'd', 'e']
return (
<div>
<input type="text"
value={this.state.input_text}
onChange={evt => this.onTextChange(evt)}/>
</div>
)
}
}

export default List
25 changes: 21 additions & 4 deletions src/TodoAddForm.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import React from 'react'

class TodoAddForm extends React.Component {
state = {
inputText: ''
constructor(props) {
super(props)
this.state = {
inputText: ''
}
}

onTextChange(evt) {
this.setState({
inputText: evt.target.value
})
}

addText(evt) {
if (this.state.inputText !== ''){
const { addTodo } = this.props
addTodo(this.state.inputText)
}
}


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

class TodoItem extends React.Component {
constructor(props) {
super(props)
}

render() {
// const {todos, deleteTodo} = this.props
const todos = this.props.todos
const deleteTodo = this.props.deleteTodo
return (
<div>

{
todos.map((item, idx) => {
return (
<div key={idx}>
<li>{item}<button onClick={evt => deleteTodo(evt)}>X</button></li>
</div>
)
})
}
</div>
);
}
8 changes: 7 additions & 1 deletion src/TodoList.jsx
Original file line number Diff line number Diff line change
@@ -2,10 +2,16 @@ import React from 'react'
import TodoItem from './TodoItem.jsx'

class TodoList extends React.Component {
constructor(props) {
super(props)
}

render() {
// const todos = this.props.todos
const {todos, deleteTodo} = this.props
return (
<div>

<TodoItem todos={todos} deleteTodo={deleteTodo}/>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map 方法應該是在這裡實作
而不是在todoitem

</div>
);
}