美文网首页
React class组件和function组件的写法

React class组件和function组件的写法

作者: key君 | 来源:发表于2019-11-08 15:44 被阅读0次
    // import React, { Component } from "react";
    // import ReactDOM from "react-dom";
    
    // import React from "./kkreact/";
    // import ReactDOM from "./kkreact/ReactDOM";
    
    import React from "./kReact";
    import ReactDOM from "./kReact/react-dom";
    
    import "./index.css";
    
    class ClassCpm extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          counter: 0,
        };
      }
      handle = () => {
        this.setState({
          counter: this.state.counter + 1,
        });
        console.log("handle", this.state.counter);
      };
      render() {
        return (
          <div className="border">
            {this.props.name}
            <button onClick={this.handle}>{this.state.counter}</button>
            {[0, 1, 2].map(item => {
              return <FuncCmp key={item} name={"function组件" + item} />;
            })}
          </div>
        );
      }
    }
    
    function FuncCmp(props) {
      return <div className="border">{props.name}</div>;
    }
    
    let jsx = (
      <div className="box border">
        <p className="border">这是React</p>
        <FuncCmp name="function组件" />
        <ClassCpm name="class组件" />
      </div>
    );
    
    ReactDOM.render(jsx, document.getElementById("root"));
    
    

    相关文章

      网友评论

          本文标题:React class组件和function组件的写法

          本文链接:https://www.haomeiwen.com/subject/mcqwbctx.html