<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<div id="app2"></div>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/prop-types.js"></script>
<script src= "../js/babel.min.js"></script>
<!--
<script type="text/babel">
class App extends React.Component {
constructor(props){
super(props)
this.state = {
todos: ['吃饭','睡觉' ,'打豆豆']
}
this.addTodo = this.addTodo.bind(this)
}
addTodo(todo){
const {todos} = this.state
todos.unshift(todo)
this.setState({todos})
}
render(){
const {todos} = this.state
return (
<div>
<h1>Simple TODO List</h1>
<Add count={todos.length} addTodo={this.addTodo}/>
<List todos={todos}/>
</div>
)
}
}
class Add extends React.Component {
constructor(props){
super(props)
this.add = this.add.bind(this)
}
add(){
const todo = this.todoinput.value.trim()
if(!todo){
return
}
this.props.addTodo(todo)
this.todoinput.value = ''
}
render(){
return (
<div>
<input type="text" ref={input => this.todoinput=input}/>
<button onClick={this.add}>add #{this.props.count+1}</button>
</div>
)
}
}
Add.propTypes = {
count: PropTypes.number.isRequired,
addTodo: PropTypes.func.isRequired
}
class List extends React.Component {
render() {
return (
<div>
<ul>
{this.props.todos.map((todo,index)=><li key={index}>{todo}</li>)}
</ul>
</div>
)
}
}
List.propTypes = {
todos:PropTypes.array.isRequired
}
ReactDOM.render(<App/> ,document.getElementById('app'))
</script>
</body>
</html>
|