Answer the question
In order to leave comments, you need to log in
How to make react distinguish between inputs and the state of a separate input changes only if something is entered in its field?
const InitStateInputs = {
total: 0,
valueA: 0,
valueB: 0
};
var Calc = React.createClass({
getInitialState: function() {
return {
initInputs: InitStateInputs
}
},
handleChange: function(e) {
this.setState({value : e.target.value});
}
,
render: function() {
return (
<div>
<input type="text" onChange={this.handleChange} value={this.state.initInputs.valueA}/>
<input type="text" onChange={this.handleChange} value={this.state.initInputs.valueB} />
</div>
)
}
})
ReactDOM.render(
<Calc />,
document.getElementById('calc')
)
Answer the question
In order to leave comments, you need to log in
It's easiest like this:
class Calc extends React.Component {
state = {
valueA: 0,
valueB: 0
};
handleChange = ({target: {name, value}}) => {
if (name) this.setState({[name]: value});
};
render() {
return (
<div onChange={this.handleChange}>
<input type='text' name='valueA' value={this.state.valueA}/>
<input type='text' name='valueB' value={this.state.valueB} />
</div>
);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question