Building a Customizable Image Slider in React Using Hooks, SCSS, and TypeScript
- Mohammad Abu Mattar
- Programming , Frontend
- 17 Feb, 2023
- 12 Mins read
Introduction
In this tutorial, we will be building a customizable image slider in React using hooks, SCSS, and TypeScript. An image slider is a common UI element used in web applications to display a set of images that can be scrolled or navigated through. With React, building an image slider becomes easier and more modular.
Hooks are a new addition to React 16.8 that allows you to use state and other React features without writing a class. Hooks enable the creation of reusable and composable logic for React components.
SCSS is a preprocessor for CSS, which makes it easier to write and maintain large stylesheets. It allows you to use variables, mixins, and functions to create more modular and maintainable styles.
TypeScript is a typed superset of JavaScript that adds type annotations and other features to make it easier to catch errors and refactor code. TypeScript improves the developer experience and makes code easier to understand and maintain.
By the end of this tutorial, you will have a basic understanding of how to create a customizable image slider in React using hooks, SCSS, and TypeScript. You will also learn how to create a reusable component that can be easily customized and styled to fit your needs.
Prerequisites
Before starting to build the customizable image slider in React, you should have a basic understanding of React, JavaScript, and CSS. You should be familiar with React hooks, including useState and useEffect, and have a working knowledge of TypeScript. Additionally, you should have Node.js and npm (Node Package Manager) installed on your machine, as we will be using them to set up the project and manage its dependencies.
If you are new to React or TypeScript, it is recommended that you first complete some beginner-level tutorials to familiarize yourself with the fundamentals of the technologies. Once you have a solid understanding of the basics, you will be better equipped to follow along with this tutorial and build your own customizable image slider.
Getting Started
n this section, we will guide you through setting up the project. To start with, we will create a new React project using vite. Vite is a fast and efficient build tool that enables you to develop your React application quickly. It is built on top of Rollup and ESBuild, which makes it a lightweight and user-friendly tool.
To create a new project, open up your terminal and run the following command:
This command will create a new React project with the name react-hooks-slider
using the react-ts
template, which includes TypeScript support. Once the project is created, navigate into the project directory using the cd
command:
This will create a new React project using TypeScript. After that, we will install the dependencie for SCSS. To do this, run the following command:
Now that we have created our project, we can begin setting up our slider component using React, Hooks, SCSS, and TypeScript. In the next section, we will start by creating a new React component for our slider.
Creating the Slider Component
The first step in building our customizable image slider is to create a React component that will render our slider. We’ll call this component Slider
.
To create the Slider
component, we’ll first need to import React and the necessary hooks from React, including useState
, useEffect
, and useRef
. We’ll also need to import any external libraries or components that we’ll be using in our slider.
Next, we’ll define our Slider function component and set up its initial state using the useState
hook. We’ll keep track of the current index of the active slide and the previous and next slide indexes. We’ll also create a ref
using the useRef
hook that will allow us to access the container element of our slider.
Then, we’ll set up the useEffect
hook to update the slider’s state and animate the slides whenever the current index changes. This hook will listen for changes to the current index and adjust the previous and next slide indexes accordingly. It will also update the position of the slider using CSS transforms to animate the slides.
Creating the Component Structure
In this section, we will create the basic structure of our slider component. We will start by creating a new folder called components
inside the src
folder. Inside the components
folder, we will create a new directory called Slider
and Icons
. The Slider
directory will contain the files for our slider component, and the Icons
directory will contain the SVG as React components.
At the end of this section, our project structure will look like this:
Now that we have created our project structure, we can start creating our slider component. To do this, at first, we will create a new file called RightArrowIcon.tsx
inside the Icons
directory. This file will contain the SVG for the right arrow icon that we will use in our slider component.
In the above code, we have created a new React component called RightArrowIcon
. This component will render the SVG for the right arrow icon. We have also added the fill
and size
props to the component, which will allow us to customize the color and size of the icon.
Next, we will create a new file called Slide.tsx
inside the Slider
directory. This file will contain the main slider component. We will start by importing the RightArrowIcon
component that we created in the previous step.
In the above code, we have created a new React component called Slide
. This component will render the main slider component. We have also added the slide
, index
, current
, and handleSlideClick
props to the component, which will allow us to pass the data for the slider and handle the click event on the slider.
Next, we will create a new file called SliderControl.tsx
inside the Slider
directory. This file will contain the main slider component. We will start by importing the Slide
component that we created in the previous step.
In the above code, we have created a new React component called SliderControl
. This component will render the slider control buttons. We have also added the type
, title
, and handleClick
props to the component, which will allow us to pass the data for the slider control buttons and handle the click event on the slider control buttons.
Next, we will create a new file called Slider.tsx
inside the Slider
directory. This file will contain the main slider component. We will start by importing the Slide
and SliderControl
components that we created in the previous steps.
In the above code, we have created a new React component called Slider
. This component will render the main slider component. We have also added the slides
and heading
props to the component, which will allow us to pass the data for the slider.
Next, we will create a new file called index.tsx
inside the Slider
directory. This file will contain the main slider component. We will start by importing the Slider
component that we created in the previous step.
In the above code, we have created a new React component called SliderComponent
. This component will render the main slider component. We have also added the slides
and heading
props to the component, which will allow us to pass the data for the slider.
Next, we will create a new file called style.scss
inside the Slider
directory. This file will contain the main slider component styles.
Running the Slider Component
Now that we have the slider component, we need to add it at App.tsx
:
Now, we can run the command npm run dev
to run the application.
After running the command, you will see the slider component in the home page:
Testing the Slider Component
To start testing the slider component, we will need to install some dependencies. Run the following command:
After installing the dependencies, we will add the jest
at package.json
:
Now that we have the slider component, we can test it. We will use the slider component in the home page. Open the src/components/Icons/RightArrowIcon.test.tsx
file and add the following code:
Now, we will add the slider component to the home page. Open the SliderControl.test.tsx
file and add the following code:
Now, we will add the slider component to the home page. Open the Slide.test.tsx
file and add the following code:
Now, we will add the slider component to the home page. Open the Slider.test.tsx
file and add the following code:
After adding the tests, we will run the tests and see if they pass, but before that, we will add the scripts
at package.json
file:
Now, we will run the tests:
Conclusion
In this tutorial, we’ve walked through the process of building a customizable image slider in React using hooks, SCSS, and TypeScript. We started by setting up our project, creating a slider component, and implementing basic functionality to switch between images. We then added options to customize the behavior and appearance of the slider, such as Wloop, and navigation buttons.
Along the way, we learned about key React concepts such as props, state, and useEffect, as well as how to use hooks to manage component state and effects. We also saw how to use SCSS to write more expressive and reusable CSS styles, and how to leverage TypeScript to add type safety and improve code readability.
We hope this tutorial has been useful and informative, and that you now feel more comfortable building React applications with hooks, SCSS, and TypeScript. Happy coding!
Source Code
You can find the source code for this tutorial on GitHub.
Live Demo
You can find the live demo for this tutorial on Vercel.