Boky Boky - 1 year ago 303
React JSX Question

Input range with React JS

I'm trying to create an input range with multiple values with React JS.

I have something like :

class price extends React.Component {

this.state = {
minValue: 0,
maxValue: 20000,
step: 1000,
firstValue: null,
secondValue: null

this.handleChange = this.handleChange.bind(this);

this.setState({firstValue: this.state.minValue, secondValue: this.state.maxValue});

handleChange(name, event){
//We set the state value depending on input that is clicked
if(name === "second"){
let newValue = parseInt(this.state.firstValue) + parseInt(this.state.step);
//The second value can't be lower than the first value
if(parseInt(this.state.secondValue) > parseInt(newValue)){

//The first value can't be greater than the second value
if(parseInt(this.state.firstValue) < parseInt(this.state.secondValue)){


const language = this.props.language;

return (
<div className="priceTitle">{language.price}</div>
<div className="rangeValues">Range : {this.state.firstValue} - {this.state.secondValue}</div>

<section className="range-slider">

<input type="range" value={this.state.firstValue} min={this.state.minValue} max={this.state.maxValue} step={this.state.step} onChange={this.handleChange.bind(this, "first")}/>
<input type="range" value={this.state.secondValue} min={this.state.minValue} max={this.state.maxValue} step={this.state.step} onChange={this.handleChange.bind(this, "second")}/>

<div className="minValue">{this.state.minValue}</div>
<div className="maxValue">{this.state.maxValue}</div>

I want to disable that second value is lower than the first value and that the first one is greater than the second one. I try to do it in handleChange function but without success.

In my example the second value can't be lower, but if the first one is for example 12000 and the second one is 13000, the state can't be updated any more. I can't change the price any more.

Any advice?

Answer Source

You have to compare your state value with the target value.

Your method should look like this

handleChange(name, event){
    let value =;
    if(name === "second"){
            if(parseInt(this.state.firstValue) < parseInt(value)){
            if(parseInt(value) < parseInt(this.state.secondValue)){
            this.setState({firstValue: value});

If you want to allow them to be equal, just use <= instead of <.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download