You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.3 KiB
61 lines
1.3 KiB
var TODO_COMPONENT = `
|
|
class TodoApp extends React.Component {
|
|
constructor(props) {
|
|
super(props);
|
|
this.handleChange = this.handleChange.bind(this);
|
|
this.handleSubmit = this.handleSubmit.bind(this);
|
|
this.state = {items: [], text: ''};
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<div>
|
|
<h3>TODO</h3>
|
|
<TodoList items={this.state.items} />
|
|
<form onSubmit={this.handleSubmit}>
|
|
<input onChange={this.handleChange} value={this.state.text} />
|
|
<button>{'Add #' + (this.state.items.length + 1)}</button>
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
handleChange(e) {
|
|
this.setState({text: e.target.value});
|
|
}
|
|
|
|
handleSubmit(e) {
|
|
e.preventDefault();
|
|
if (!this.state.text.length) {
|
|
return;
|
|
}
|
|
var newItem = {
|
|
text: this.state.text,
|
|
id: Date.now()
|
|
};
|
|
this.setState((prevState) => ({
|
|
items: prevState.items.concat(newItem),
|
|
text: ''
|
|
}));
|
|
}
|
|
}
|
|
|
|
class TodoList extends React.Component {
|
|
render() {
|
|
return (
|
|
<ul>
|
|
{this.props.items.map(item => (
|
|
<li key={item.id}>{item.text}</li>
|
|
))}
|
|
</ul>
|
|
);
|
|
}
|
|
}
|
|
|
|
ReactDOM.render(<TodoApp />, mountNode);
|
|
`.trim();
|
|
|
|
ReactDOM.render(
|
|
<ReactPlayground codeText={TODO_COMPONENT} />,
|
|
document.getElementById('todoExample')
|
|
);
|
|
|