react string convert to int
语法:
const intValue = parseInt(this.state.inputValue, 10);完整的示例:
import React from 'react';
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
inputValue: '',
intValue: 0
};
}
handleChange = (event) => {
this.setState({
inputValue: event.target.value
});
}
convertToInt = () => {
const intValue = parseInt(this.state.inputValue, 10);
if (!isNaN(intValue)) {
this.setState({
intValue: intValue
});
}
}
render() {
return (
<div>
<input type="text" value={this.state.inputValue} onChange={this.handleChange} />
<button onClick={this.convertToInt}>Convert to Int</button>
<p>Integer value: {this.state.intValue}</p>
</div>
);
}
}
export default MyComponent;