score:2
You should parse the int as the value is getting read from the event.target.value which will always give you string
handleChange = (e) => {
const { name, value } = e.target
this.setState({ [name]: parseInt(value, 10) })
}
score:0
You can use parseInt
when use the setState:
handleChange = (e) => {
const { name, value } = e.target
this.setState({ [name]: parseInt(value) })
}
But maybe is necessary to valid the value before execute parseInt, because can happen a error like Received NaN for the
valueattribute. If this is expected, cast the value to a string.
So maybe with try/catch
can solve your problem:
handleChange = (e) => {
const { name, value } = e.target
try {
this.setState({ [name]: parseInt(value) })
} catch (e) {
this.setState({ [name]: 0})
}
}
score:0
class NumberForm extends Component {
state = {
myNumber:parseInt('')
}
handleChange = (e) => {
const { name, value } = e.target
this.setState({ [name]: value })
//this.setState({ [name]: parseInt(value, 10) })
}
render() {
return (
<form>
<input
type="number"
name="myNumber"
value={this.state.myNumber}
onChange={this.handleChange}
/>
</form>
);
}
}
This could works for starting your state and html element in empty
score:0
Here we GO SOLUTION for this problem is you can parse the value of state in handler function.
const handleChange = (event) => {
this.setState({ [event.target.name]: parseInt(event.target.value, 10) })
}
Source: stackoverflow.com
Related Query
- Initializing empty state that will receive a number in React
- In React Hooks. How to set state to a random number and use that to display a string from an array
- Guess the number with React - what is the proper way of using state in that case
- How to return column that will be sorted by number within component in React Table?
- I want to update the state object in immutable manner so that react and redux will work properly?
- When testing, code that causes React state updates should be wrapped into act
- Typescript and React setting initial state with empty typed array
- Initializing React number input control with blank value?
- React shouldComponentUpdate() is called even when props or state for that component did not change
- React router v4 - How do you access the state that is passed via Redirect?
- React: If you set state in a request animation frame will react schedule rendering on the animation frame?
- How to TEST async calls made in componentDidMount that set the state of React Component
- Will a react component reset its state when it re-renders?
- How to test React state after calling a component method that updates state - using Enzyme
- Initializing react component state
- is there a good react carousel components that will work with gatsby-image?
- Set state in child after it will receive props?
- How do I manage state on a React component that can have state changed from the parent or from events upon it?
- React Hooks (Rendering Arrays) - Parent component holding a reference of children that are mapped vs Parent component holding the state of children
- How to update state that has dependency on other state with React Hooks
- React & Redux - state getting empty after route changed
- React - will not re render on state change
- Is it a bad practice to use state in a React component if the data will not change? Should I use a property on the class instead?
- How do I pass a React Context's state through an onClick function created in a method inside that context class?
- React Input validation if empty or number
- React - Navigation that will smooth scroll to section of the page
- Too many re-renders. React limits the number of renders to prevent an infinite loop. Updating state of a functional component inside the render method
- I need to execute a function that will update the state right after using setState hook, but state inside function is empty?
- Server side rendering React components that loads state through Ajax
- React testing library + Jest: How to test a component that receives a number then format it
More Query from same tag
- How to create react app using create-react-app with reactjs 15 version
- Material UI CSS Animation for underline
- React pdf responsiveness using React Hooks, but when increasing or decreasing window size I get this TypeError
- How to make React component wait for redux store variables to get set?
- How to set iframe content of a react component
- browserify and reactify multiple source and destination files
- Ant Design error: "Form.create() getFieldDecorator is not a function"
- React-redux-firebase: Uncaught Error: Supplied prop/s "dispatch" are reserved for internal firebaseConnect() usage
- Trouble changing toggle value from active to not active
- Function to format and add object to array not functioning properly
- How to add fade transitions in between state changes
- Is there any React Carousel library support load more (fetch new items from server) when users move to the last item?
- Why my colors is not output on the screen under a rafce react environtment
- React: Inline if doesn't resolve to false when value is 'undefined'
- How to remove console errors from iframe in react.js page
- history.push using react-router-dom
- How to employ the useState() hook with config props in a Docusaurus project?
- this keyword behavior in classes and react component ( Arrow function vs regular function)
- What am I doing wrong that justify-content doesn't have the expected behavior?
- Ionic 4 React hamburger menu
- React app : old version kept in cache until I inspect it
- How to add defaultOption to AsyncSelect?
- If/else statement inside loop inside return statement?
- How to call loading function with React useEffect only once
- Passing Arguments to Event Handlers
- How to make a table responsive in Semantic-UI-React?
- how can i create a helper type function in my redux module to return a filtered set of readonly data from state?
- How to fireEvent.scroll on a element inside container with react-testing-library?
- Appending HTML to component and React Component class unexpected token, expected }
- One fetch request creates many GET requests on server