Skip to content

React类组件Demo

文件后缀

shell
*.jsx

Demo

javascript
import React, {Component} from 'react';

class Timer extends Component {
    constructor(props) {
        super(props);
        this.state = {seconds: 0};
    }

    componentDidMount() {
        this.interval = setInterval(() => {
            this.setState(state => ({
                seconds: state.seconds + 1,
            }));
        }, 1000);
    }

    componentWillUnmount() {
        clearInterval(this.interval);
    }

    render() {
        return <h1>Seconds: {this.state.seconds}</h1>;
    }
}