Here are two common “sliders” you might mean in React:
- Image/Content Carousel
- Range/Input Slider
Pick the one that fits your use‑case (or let me know if you need something else)!
1. Image/Content Carousel (using react‑slick)
“react‑slick” is a battle‑tested carousel component.
a) Install
npm install react-slick slick-carousel
# or
yarn add react-slick slick-carousel
b) Import CSS (e.g. in index.js or App.js)
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
c) Basic Usage
import Slider from "react-slick";
const images = [
"https://via.placeholder.com/600x300?text=Slide+1",
"https://via.placeholder.com/600x300?text=Slide+2",
"https://via.placeholder.com/600x300?text=Slide+3",
];
function ImageCarousel() {
const settings = {
dots: true,
infinite: true,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1,
autoplay: true,
autoplaySpeed: 3000,
};
return (
<div style={{ width: 600, margin: "0 auto" }}>
<Slider {...settings}>
{images.map((src, idx) => (
<div key={idx}>
<img src={src} alt={`Slide ${idx + 1}`} style={{ width: "100%" }} />
</div>
))}
</Slider>
</div>
);
}
export default ImageCarousel;
d) Custom arrows (optional)
function NextArrow(props) {
const { onClick } = props;
return <div className="next-arrow" onClick={onClick}>→</div>;
}
function PrevArrow(props) {
const { onClick } = props;
return <div className="prev-arrow" onClick={onClick}>←</div>;
}
// then in settings:
const settings = {
/* … */
nextArrow: <NextArrow />,
prevArrow: <PrevArrow />
};
2. Range/Input Slider
If you simply want a numeric “range” slider (e.g. volume control), you can either use the native <input type="range"> or a library like “react-slider.”
a) Native HTML range
function VolumeControl() {
const [value, setValue] = React.useState(50);
return (
<div style={{ width: 300, margin: "2rem auto" }}>
<label>Volume: {value}%</label>
<input
type="range"
min="0" max="100"
value={value}
onChange={(e) => setValue(e.target.value)}
style={{ width: "100%" }}
/>
</div>
);
}
export default VolumeControl;
You can restyle the slider track/thumb in CSS:
input[type="range"] {
-webkit-appearance: none;
width: 100%;
height: 8px;
background: #ddd;
border-radius: 4px;
outline: none;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
width: 20px; height: 20px;
background: #007bff;
border-radius: 50%;
cursor: pointer;
}
b) Using “react-slider”
For more advanced styling & multiple thumbs:
npm install react-slider
# or
yarn add react-slider
import ReactSlider from "react-slider";
function DualThumbSlider() {
const [value, setValue] = React.useState([20, 80]);
return (
<div style={{ width: 300, margin: "2rem auto" }}>
<ReactSlider
value={value}
onChange={setValue}
min={0}
max={100}
pearling
minDistance={10}
renderTrack={(props, state) => (
<div
{...props}
style={{
...props.style,
height: "6px",
background:
state.index === 1 ? "#0f0" : "#ddd",
}}
/>
)}
renderThumb={(props) => (
<div
{...props}
style={{
...props.style,
height: "16px",
width: "16px",
backgroundColor: "#007bff",
borderRadius: "50%",
}}
/>
)}
/>
<p>
Selected range: {value[0]} – {value[1]}
</p>
</div>
);
}
export default DualThumbSlider;
Let me know which one you’re after (or if you need more custom behavior) and I can help you tweak it further! 😊