javascript
64 lines · 8 steps
Building a dual-thumb price slider in React
A two-handle range slider that keeps its min and max from ever crossing, using clamped state updates.
Explained by
highlit
1import { useCallback, useState } from "react";
2
3const MIN = 0;
4const MAX = 1000;
5const GAP = 50;
6
7export default function PriceRangeSlider({ onChange }) {
8 const [range, setRange] = useState({ min: 200, max: 800 });
9
10 const updateMin = useCallback((value) => {
11 setRange((prev) => {
12 const next = Math.min(Math.max(value, MIN), prev.max - GAP);
13 const updated = { ...prev, min: next };
14 onChange?.(updated);
15 return updated;
16 });
17 }, [onChange]);
18
19 const updateMax = useCallback((value) => {
20 setRange((prev) => {
21 const next = Math.max(Math.min(value, MAX), prev.min + GAP);
22 const updated = { ...prev, max: next };
23 onChange?.(updated);
24 return updated;
25 });
26 }, [onChange]);
27
28 const toPercent = (value) => ((value - MIN) / (MAX - MIN)) * 100;
29
30 return (
31 <div className="range-slider">
32 <div className="range-slider__track">
33 <div
34 className="range-slider__fill"
35 style={{
36 left: `${toPercent(range.min)}%`,
37 right: `${100 - toPercent(range.max)}%`,
38 }}
39 />
40 </div>
41 <input
42 type="range"
43 min={MIN}
44 max={MAX}
45 value={range.min}
46 onChange={(e) => updateMin(Number(e.target.value))}
47 className="range-slider__thumb range-slider__thumb--min"
48 aria-label="Minimum price"
49 />
50 <input
51 type="range"
52 min={MIN}
53 max={MAX}
54 value={range.max}
55 onChange={(e) => updateMax(Number(e.target.value))}
56 className="range-slider__thumb range-slider__thumb--max"
57 aria-label="Maximum price"
58 />
59 <output className="range-slider__values">
60 ${range.min} – ${range.max}
61 </output>
62 </div>
63 );
64}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Clamping values inside the state updater keeps two dependent handles from crossing each other.
- 2Deriving both handles from one state object keeps the min and max always consistent.
- 3Optional-chaining a callback lets a component report changes without requiring the parent to supply a handler.
Related explainers
javascript
class LyricsSync { constructor(audio, container, lines) { this.audio = audio; this.container = container;
Building a synced lyrics highlighter
binary-search
dom-manipulation
event-handling
Intermediate
9 steps
javascript
const express = require('express'); const app = express();
Enforcing HTTPS with Express middleware
middleware
https
security
Intermediate
6 steps
javascript
const express = require('express'); const router = express.Router(); router.get('/articles/:slug', async (req, res, next) => {
Conditional GET caching in Express
http-caching
conditional-get
routing
Intermediate
8 steps
javascript
function parseHexColor(hex) { const cleaned = hex.trim().replace(/^#/, ''); const expand = (short) =>
Parsing hex colors into RGBA channels
parsing
bitwise
regex
Intermediate
7 steps
javascript
import { useReducer, useCallback } from 'react'; function historyReducer(state, action) { const { past, present, future } = state;
Undo/redo form state with a React reducer
undo-redo
reducer
immutability
Intermediate
10 steps
javascript
import { NextResponse } from 'next/server'; import { db } from '@/lib/db'; function csvCell(value) {
Streaming a CSV export in a Next.js route
streaming
csv
readablestream
Advanced
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-a-dual-thumb-price-slider-in-react-explained-javascript-ea1f/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.