Skip to content
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

2017-ncueie-isomorphic #1

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
23 changes: 22 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,32 @@ import TodoList from './TodoList.jsx'
import TodoAddForm from './TodoAddForm.jsx'

class TodoApp extends React.Component {

constructor(props) {
super(props);
}

state = {
list: ['DefauleItem1','DefauleItem2']
}

removeElement = (idx) => {
//不想用setState所以用forceUpdate
this.state.list.splice(idx,1);
Copy link

Choose a reason for hiding this comment

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

盡量不要用forceupdate喇

Copy link
Author

Choose a reason for hiding this comment

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

this.setState({...this.state}) //可是我覺得這樣很沒有freestyle

this.forceUpdate();
}

addElement = (text) => {
this.state.list.push(text);
this.forceUpdate();
}

render() {
return (
<div>
<h2>Todo App</h2>
123
<TodoAddForm addElement={this.addElement} />
<TodoList list={this.state.list} removeElement={this.removeElement} />
</div>
);
}
Expand Down
27 changes: 24 additions & 3 deletions src/TodoAddForm.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
import React from 'react'

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

state = {
inputText: ''
}


handleChange = (event) => {
this.setState({ ...this.state, inputText: event.target.value })
}

handleClick = (event) => {
// pass userinput to the parent component
this.props.addElement(this.state.inputText);
// clear userinput
this.setState({ ...this.state, inputText: "" });
// focus on input
this.nameInput.focus();
}

componentDidMount() {
this.nameInput.focus();
}

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

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

handleClick = (event) => {
this.props.removeElement(this.props.idx);
}

render() {
return (
<div>

<div style={{marginTop:"5px"}}>
<button onClick={this.handleClick}> 刪除 </button>
<a style={{marginLeft:"5px"}}>{this.props.text}</a>
</div>
);
}
Expand Down
14 changes: 12 additions & 2 deletions src/TodoList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ import React from 'react'
import TodoItem from './TodoItem.jsx'

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

render() {
return (
<div>

<div style={{marginTop:"15px"}}>
{
this.props.list.map(
(ele, index, array) => {
return (<TodoItem text={ele} idx={index} key={index} removeElement={this.props.removeElement}/>);
}
)
}
</div>
);
}
Expand Down