javascript - react 计算器 : How to prevent multiple decimals?

标签 javascript reactjs calculator

我几乎已经使用 React 构建了一个简单的计算器。 我只是在处理多个小数时遇到麻烦。我试图做的是写一个条件,但它不起作用。你能帮我一下吗?

这是我的代码的一部分:

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            input: '0'
        };
    }


    addToInput = e => {
        const value = e.target.value;
        const oldValue = this.state.input;

        if (this.state.input != '0') {
            this.setState({ input: (this.state.input + value) });

        } else if (value == '.' && oldValue.includes('.')) {
            console.log('Mulitple decimals');
        } else {
            this.setState({ input: value });
        }
    };

    render() {
        return (<Button addToInput={ this.addToInput } />);
    }
}

class Button extends React.Component {

    render() {
        return (

            <div className="row">
                <button
                    value="."
                    id="decimal"
                    onClick={ this.props.addToInput }
                >.
                </button>
                <button
                    value="0"
                    id="zero"
                    onClick={ this.props.addToInput }
                >0
                </button>
                <button
                    value="-"
                    id="subtract"
                    onClick={ this.props.addToInput }
                >-
                </button>
            </div>


        );
    }
}

提前谢谢您!

最佳答案

像这样改变你的addToInput:

    addToInput = e => {
        const value = e.target.value;
        const oldValue = this.state.input;

        if (value === '.' && oldValue.includes('.')) {
            console.log('Mulitple decimals');
            return;
        }

        if (this.state.input !== '0') {
            this.setState({ input: (this.state.input + value) });

        } else {
            this.setState({ input: value });
        }
    };

为什么您遇到问题:

    addToInput = e => {
        const value = e.target.value;
        const oldValue = this.state.input;

        if (this.state.input !== '0') {
            // after first addToInput you will end up here ALWAYS
            this.setState({ input: (this.state.input + value) });
        } else if (value === '.' && oldValue.includes('.')) {
            // You will never be here because this.state.input !== '0' is always true after first addToInput
            console.log('Mulitple decimals'); 
        } else {
            // you will end up here only when you lunch your addToInput first time
            this.setState({ input: value });
        }
    };

关于javascript - react 计算器 : How to prevent multiple decimals?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58399297/

相关文章:

iphone - 为什么我的 NSRange 总是 0?

C++ 对我的命令行计算器进行故障排除

javascript回调函数同步

javascript - 为什么exportRoot不使用createjs和animate cc将movieclip放置在 Canvas 上?

javascript - 是否可以在 React 之外获取 React 属性?

reactjs - 如何将焦点设置在 FormControl - Select 上? ( react )

java - 计算器开关 Java 基础

javascript - 我已经检索了动态表的高度,现在我需要将该高度放入另一个元素中

javascript - boolean 相等

javascript - 语义 UI React : How to Hide Suggested Text from Search Box