so I have this code
handleChange(event) {
if (event.target.value === '' || parseInt(event.target.value)) {
this.props.dispatch({
type: "SET_INT_INPUT_METHOD",
payload: event.target.value === '' ? '' : parseInt(event.target.value)
})
}
}
I would like to get input only Integers so how would I implement it in here ?
snigo
December 10, 2019, 11:13am
2
Quickest fix would be:
if (event.target.value === '' || parseInt(event.target.value) === event.target.value)
Could you explain a little bit why and how it works I am afraid I don’t quite get it
snigo:
Quickest fix would be:
It doesn’t work, I tested it and it doesn’t record any input I just get the error that there is nothing there.
Fixed it like this
handleChange(event) {
if (event.target.value === '' || parseInt(event.target.value)) {
this.props.dispatch({
type: "SET_INT_INPUT_METHOD",
payload: isNaN(parseInt(event.target.value)) ? '' : parseInt(event.target.value)
})
}
}
snigo
December 10, 2019, 9:20pm
6
Consider following:
parseInt(0); // 0
parseInt(45andanythingthatfollows); // 45
I suppose both options are unacceptable for you
Now, your question wasn’t about fixing your dispatch function, was it?
You can just dispatch event.target.value
, because you’re already checking value above…