I am using an increment (count) for not to click the period (.) second time. So once the period is clicked then second time it skips. I used the example from Incrementing state value by one using React, but the count is not incrementing.
const InitVal = ({ strValue, handleClick }) => (
<div>
{strValue.map((item) => (
<button onClick={() => handleClick(item.key)}>{item.key}</button>
))}
</div>
);
class App extends React.Component {
constructor(props) {
super(props);
this.state = {strValue: [{ key: '7' },{ key: '8' },{ key: '9' },{ key: '4' },{ key: '5' },{ key: '6' },{ key: '1' },{ key: '2' },{ key: '3' },{ key: '0' },{key: '.'}],value: '0',count: 0,};
this.handleClick = this.handleClick.bind(this);
}
handleClick(key) {
const { value } = this.state;
const { count } = this.state;
const digNprd = /[0-9.]/
if (value.charAt(0) === "0") {
this.setState({ value: `${key}` })
} else if (digNprd.test(key)) {
this.setState((u) => {
if (key === '.') {
if (u.count < 1) {
count: u.count + 1
} else {
key = ''
}
}
return { value: `${value}${key}` }
})
}
}
render() {
return (
<div><br /><InitVal strValue={this.state.strValue} handleClick={this.handleClick} /> <br /> <div>value: {this.state.value}</div><br />
<div>count: {this.state.count}</div>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.0/umd/react-dom.production.min.js"></script>
<div id='root'></div>