S
S
sergemin2017-08-27 14:33:15
React
sergemin, 2017-08-27 14:33:15

Why does the code run one step late? Where is the mistake?

I am writing a calculator in react

constructor() {
        super();
        this.state = {
            valueInput: 0,
            firstOperand: '',
            secondOperand: '',
            result: '',
        }
        this.renderButton = this.renderButton.bind(this);
        this.refreshInput = this.refreshInput.bind(this);
    }
refreshInput(event) {
        const target = event.target.innerHTML;
        //if you clicked a number
        if(!isNaN(parseFloat(target)) && isFinite(target)) {
            this.setState({
                firstOperand: this.state.firstOperand + target,
                valueInput: this.state.firstOperand,
            })
        }
    }

The refreshInput function hangs as a handler on all calculator buttons. Why is there a delay of 1 step when updating the output of the value in input
That is, it turns out that I press the button, and the input is reset, then I press the button a second time - then the value of the first press is displayed, etc.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Alexandrovich, 2017-08-27
@sergemin

refreshInput(event) {
        const target = event.target.innerHTML;
        //if you clicked a number
        if(!isNaN(parseFloat(target)) && isFinite(target)) {
            let number = this.state.firstOperand + target
            this.setState({
                firstOperand: number,
                valueInput: number,
            })
        }
    }

setState does not work immediately

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question