javascript - setState from input on change - can't delete from input -
i have input
:
<input type="text" value={this.state.current} onchange={this.handlechange.bind(this)} />
handlechange()
method:
handlechange(e) { let val = parseint(e.target.value); if (val) { this.set(val); } }
and set()
method (simple set number between 1 , 5 state):
set(val = 1) { if (val < 1) { val = 1; } if (val > 5) { val = 5; } this.setstate({ current: val }); }
my problem is: when want change value in input, can't delete number it. think auto filled state imediatly after delete number. problem in code?
exmaple on jsfiddle: https://jsfiddle.net/4sc3p2ny/
when trying remove value input, e.target.value
returns ''
in case parseint(e.target.value)
returns nan
, because parseint('')
returns nan
., need check , set current
empty string
handlechange(e) { let val = parseint(e.target.value); if (isnan(val)) { this.setstate({ current: '' }); } else { if (val < 1) { val = 1; } if (val) { this.set(val); } } }
Comments
Post a Comment