How to Create Custom Arrows using react-slick in ReactJS

share link

by vsasikalabe dot icon Updated: Mar 28, 2023

technology logo
technology logo

Solution Kit Solution Kit  

React Slick gives a better component that we can use to add a carousel to our React applications. We can design the Slider component and apply styling according to our requirements. React Slick is an excellent library in react for creating carousels. It provides accessibility and responsiveness, amongst other attributes, to help you create significant carousels. We can also import CSS properties from the slick-carousel package that provides default styling. After that, we use the Slider component to provide a carousel with slides. Slick is a feature-rich slider plugin for producing highly customizable, fully responsive, and mobile-friendly carousels/sliders that work with HTML elements. 

 

Slider settings have three configurations which are given below: 

  • slidesToShow — number to regulate the number of slides to keep in view 
  • slidesToScroll — number to determine the number of slides to move when navigating the carousel 
  • infinite — Boolean to determine if the carousel continues in a loop when the last item is reached 

The two functions we always want to use from the API are slickPrev and slickNext, which move the carousel back and forth, respectively by the number specified in slidesToScroll. 

 

Here is an example of how to create custom arrows using react-slick in Reactjs: 

Preview of the output that you will get on running this code from your IDE

Code

In this solution we use the React library.

import React, { Component } from "react";
import Slider from "react-slick";

function SampleNextArrow(props) {
  const { className, style, onClick } = props;
  return (
    <div
      className={className}
      style={{ ...style, display: "block", background: "red" }}
      onClick={onClick}
    />
  );
}

function SamplePrevArrow(props) {
  const { className, style, onClick } = props;
  return (
    <div
      className={className}
      style={{ ...style, display: "block", background: "green" }}
      onClick={onClick}
    />
  );
}

export default class CustomArrows extends Component {
  render() {
    const settings = {
      dots: true,
      infinite: true,
      slidesToShow: 3,
      slidesToScroll: 1,
      nextArrow: <SampleNextArrow />,
      prevArrow: <SamplePrevArrow />
    };
    return (
      <div>
        <h2>Custom Arrows</h2>
        <Slider {...settings}>
          <div>
            <h3>1</h3>
          </div>
          <div>
            <h3>2</h3>
          </div>
          <div>
            <h3>3</h3>
          </div>
          <div>
            <h3>4</h3>
          </div>
          <div>
            <h3>5</h3>
          </div>
          <div>
            <h3>6</h3>
          </div>
        </Slider>
      </div>
    );
  }
}

import React, { Component } from "react";
import Slider from "react-slick";

export default class PreviousNextMethods extends Component {
  constructor(props) {
    super(props);
    this.next = this.next.bind(this);
    this.previous = this.previous.bind(this);
  }
  next() {
    this.slider.slickNext();
  }
  previous() {
    this.slider.slickPrev();
  }
  render() {
    const settings = {
      dots: true,
      infinite: true,
      speed: 500,
      slidesToShow: 1,
      slidesToScroll: 1
    };
    return (
      <div>
        <h2>Previous and Next methods</h2>
        <Slider ref={c => (this.slider = c)} {...settings}>
          <div key={1}>
            <h3>1</h3>
          </div>
          <div key={2}>
            <h3>2</h3>
          </div>
          <div key={3}>
            <h3>3</h3>
          </div>
          <div key={4}>
            <h3>4</h3>
          </div>
          <div key={5}>
            <h3>5</h3>
          </div>
          <div key={6}>
            <h3>6</h3>
          </div>
        </Slider>
        <div style={{ textAlign: "center" }}>
          <button className="button" onClick={this.previous}>
            Previous
          </button>
          <button className="button" onClick={this.next}>
            Next
          </button>
        </div>
      </div>
    );
  }
}

Instructions

Follow the steps carefully to get the output easily.

  1. Install the Node.js and React on your IDE. (preferably Visual Studio Code )
  2. Create React application using npx create-react-app foldername
  3. cd foldername.
  4. npm i react-slick
  5. Click on the code, and we get two types of code. Here we took the second one. Copy the code using the "Copy" button above, and paste it into App.js. ( Remove the earlier code from App.js)
  6. npm start to run the file.


I hope you found this useful. I have added the link to dependent libraries, and version information in the following sections.


I found this code snippet by searching for 'Custom Arrows using react-slick in Reactjs' in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in VS Code 1.73.1 version.
  2. The solution is tested on Nodejs 16.14.2 version.
  3. react 18.2.0 version.
  4. react-slick 0.29.0 version


Using this solution, we are able to Rendering Custom Arrows using react-slick in Reactjs with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to Rendering Custom Arrows using react-slick in Reactjs.

Dependent Libraries

create-react-appby facebook

JavaScript doticonstar image 100082 doticonVersion:v5.0.1doticon
License: Permissive (MIT)

Set up a modern web app by running one command.

Support
    Quality
      Security
        License
          Reuse

            create-react-appby facebook

            JavaScript doticon star image 100082 doticonVersion:v5.0.1doticon License: Permissive (MIT)

            Set up a modern web app by running one command.
            Support
              Quality
                Security
                  License
                    Reuse

                      You can search for any dependent library on kandi like 'react'.

                      Support

                      1. For any support on kandi solution kits, please use the chat
                      2. For further learning resources, visit the Open Weaver Community learning page

                      See similar Kits and Libraries