zoukankan      html  css  js  c++  java
  • React在html中的应用

    <!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>
      <script src="./node_modules/react/umd/react.development.js"></script>//npm i react
      <script src="./node_modules/react-dom/umd/react-dom.development.js"></script>// npm i react
      <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
      <script src="./node_modules/prop-types/prop-types.min.js"></script>// npm install i prop-types
      <script type="text/babel">
        class App extends React.Component {
          constructor(props) {
            super(props);
            this.state = {
              todos: ['AAA', 'BBB', 'CCC']
            };
            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>
                Simple TODO list
                <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() {
            const { todos } = this.props
            return (
              <ul>
                {todos.map((todo, key) => (<li key={key}>{todo}</li>))}
              </ul>
            )
          }
        }
        List.propTypes = {
          todos: PropTypes.array.isRequired
        }
        ReactDOM.render(<App />, document.getElementById('app'));
      </script>
    </body>
    
    </html>
    

      

  • 相关阅读:
    SQL SERVER 2008的数据压缩
    protected,internal和protected internal
    CSS笔记
    太吓人了!妈妈必看:国内人贩子抢孩子竟使出狠招
    ASP.NET上传图片的简单方法
    VS2005快捷键大全
    判断ExecuteScalar()是否返回结果
    AppSettings和ConnectionStrings的区别
    VSS中的签入和签出
    对目前工作烦躁的人来看看,你真正明白多少
  • 原文地址:https://www.cnblogs.com/junechen/p/14388204.html
Copyright © 2011-2022 走看看