work hard work smart

专注于AI+Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WebStorm 创建react工程

Posted on 2026-08-30 14:03  work hard work smart  阅读(5)  评论(0)    收藏  举报

 

工程名称为reactdemojs

image

 文件结构如下

image

 

创建一个父组件和子组件

父组件为

import React from  "react";

import Child from "./Child";

export default class Life  extends React.Component{

    constructor(props) {
        super(props);
        this.state = {
            count:0
        };
    }

    handleAdd=() => {
        this.setState(
            {
                count: this.state.count +=1
            }
        )
    }

    render() {
        return <div>
            <p>react生命周期 这里是父组件</p>
            <button onClick={this.handleAdd}>点击一下</button>
            <p>{this.state.count}</p>
            <Child name="Tom" ></Child>
        </div>
    }

}

  

子组件为Child

import React from  "react";

export default class Child  extends React.Component{

    constructor(props) {
        super(props);
        this.state = {
            count : 0
        }
    }

    componentDidMount() {
        console.log("did mount");
    }


    shouldComponentUpdate(nextProps, nextState, nextContext) {
       console.log("should update")
        return true;
    }



    render() {
        return <div>
            <p>这里是子组件</p>
            <p>{this.props.name}</p>
        </div>
    }

}

  

运行后效果为

image