3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【React】コンテンツをスライドさせて表示したい

Last updated at Posted at 2024-02-15

はじめに

react-slickを使って自動でスライド送りするような動きを実装してみます

やりかた

yarn add react-slick
yarn add slick-carousel

次に公式に記載されているコードを参考にコンポーネントを実装します。
(この記事では少し変えています。cssは適用していないです。)

コンポーネント
import React from "react";
import Slider from "react-slick";
import 'slick-carousel/slick/slick.css';
import 'slick-carousel/slick/slick-theme.css';

import a from '../assets/images/a.jpg';
import b from '../assets/images/b.jpg';
import c from '../assets/images/c.jpg';

function ViewSlide() {
  const settings = {
    dots: true,
    infinite: true,
    slidesToShow: 3,
    slidesToScroll: 1,
    autoplay: true,
    speed: 2000,
    autoplaySpeed: 5000,
    cssEase: "linear",
    pauseOnHover: false,
  };
  return (
      <Slider {...settings}>
      <img src= {a} alt="Slide a"></img>
        <img src= {b} alt="Slide b"></img>
        <img src= {c} alt="Slide c"></img>
      </Slider>
  );
}


export default ViewSlide;

react-slickをインストールして実装をすると、以下のエラーが発生しました

l_9007702_1840_58116007703a6480698f93068c83b65e.png

プロジェクト配下にreact-slick.d.tsファイルを作成し、ファイルに以下を記載することで解決しました。

declare module 'react-slick';

おわりに

簡単でした!

参考記事

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?