0

Нужно сделать как в коде, но только без использования setInterval Напишите какие есть варианты решения этой проблемы.

import React, {Component} from 'react';
import styles from "./Count.module.css";
import RandomList from "../RandomList/index";


class Counter extends Component {
    constructor(props) {
        super(props);
        this.state = {
            counter: 0,
            arr: []
        };
    }

    inc = () => {
        this.setState({
            counter: this.state.counter + 1
        });
        this.generateNumbers();
    };


    generateNumbers() {
        const {arr} = this.state;
        const min = 0;
        const max = 101;
        let number = Math.round(Math.random() * (max - min) + min);
        arr.push(number);
        this.setState({arr: arr});
    }


    componentDidMount() {
        this.interval = setInterval(() =>
            this.inc(),3000);
    }

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


    render() {
        const {arr} = this.state;
        const renderItems = arr.map((item) => {
            return (
                <li>{item}</li>
            )
        });

        // setTimeout(this.inc, 1000);

        return (

            <div>
                <button onClick={this.inc}>Инкрементировать</button>
                <h1>Кол-во нажатий:{this.state.counter}</h1>
                <h1>Случайные числа:
                    <ul>
                        {renderItems}
                    </ul>
                </h1>

            </div>
        );
    }
}


export default Counter;

1
  • Непонятно чем вас не устраивает setInterval
    – Alexey Ten
    25 авг 2019 в 13:38

1 ответ 1

0

1) Деревянно:

import React, {Component} from 'react';
import styles from "./Count.module.css";
import RandomList from "../RandomList/index";


class Counter extends Component {
    constructor(props) {
        super(props);
        this.state = {
            counter: 0,
            arr: []
        };
    }

    inc = () => {
        this.setState({
            counter: this.state.counter + 1
        });
        this.generateNumbers();
        this.timer = setTimeout(this.inc, 3000);
    };


    generateNumbers() {
        const {arr} = this.state;
        const min = 0;
        const max = 101;
        let number = Math.round(Math.random() * (max - min) + min);
        arr.push(number);
        this.setState({arr: arr});
    }


    componentDidMount() {
        this.timerId = setTimeout(this.inc, 3000);
    }

    componentWillUnmount() {
        clearTimeout(this.timerId);
    }


    render() {
        const {arr} = this.state;
        const renderItems = arr.map((item) => {
            return (
                <li>{item}</li>
            )
        });

        return (

            <div>
                <button onClick={this.inc}>Инкрементировать</button>
                <h1>Кол-во нажатий:{this.state.counter}</h1>
                <h1>Случайные числа:
                    <ul>
                        {renderItems}
                    </ul>
                </h1>

            </div>
        );
    }
}


export default Counter;

2) С библиотекой:

lodash: https://lodash.com/docs/4.17.15#debounce

3) Нормально:

Если работает - ничего не меняй

1

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.