๐Ÿค ์€์ง€log ๐Ÿค

[REACT] Carousel๋กœ ๋ฐฐ๋„ˆ ์Šฌ๋ผ์ด๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ ๋ณธ๋ฌธ

๐Ÿ’™ React

[REACT] Carousel๋กœ ๋ฐฐ๋„ˆ ์Šฌ๋ผ์ด๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ

Eun_zii 2023. 2. 6. 12:55

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  { useState ์™€ useEffect } ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ง์ ‘ ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ๋กœ ํ•˜์˜€๋‹ค ! ๐Ÿฅ

๊ทธ์ „์—๋Š” react-bootstrap์œผ๋กœ๋งŒ ํ•ด๋ดค๋˜ํ„ฐ๋ผ,, ์กฐ๊ธˆ ๊ฑฑ์ •ํ–ˆ๋Š”๋ฐ

์—ญ์‹œ๋‚˜ ์–ด๋ ค์› ์ง€๋งŒ ์ •๋ณด๋„ ๋งŽ์•˜๊ณ , ์ด๊ฒƒ ์ €๊ฒƒ ์ƒˆ๋กœ ์‹œ๋„ํ•ด๋ณด๋Š” ์žฌ๋ฏธ๋„์žˆ์—ˆ๋‹ค -๐Ÿ’™

 

๐Ÿ”† ๊ตฌํ˜„ ์ฝ”๋“œ

import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import styled from "styled-components";

import { SLIDE_DATA } from "datas/slide";

const Banner = () => {
  const [currentSlide, setCurrentSlide] = useState(0);
  const handleSlide = (id: number) => setCurrentSlide(id);

  useEffect(() => {
    setTimeout(() => {
      if (currentSlide >= SLIDE_DATA.length - 1) {
        return setCurrentSlide(0);
      }
      return setCurrentSlide((prev) => prev + 1);
    }, 3000);
  }, [currentSlide]);

  return (
    <Container>
      <SliderContainer index={currentSlide}>
        {SLIDE_DATA.map(({ id, title, img }) => (
            <img src={img} alt="" />
              <Title>
                <p>{title}</p>
              </Title>
          </CustomLink>
        ))}
      </SliderContainer>
      <ButtonWrap>
        {SLIDE_DATA.map(({ id }) => (
          <Button
            key={id}
            onClick={() => handleSlide(id)}
            isActive={id === currentSlide}
          />
        ))}
      </ButtonWrap>
    </Container>
  );
};

const Container = styled.div`
  margin: 0 auto;
  width: 100%;
  overflow: hidden;
`;
const SliderContainer = styled.div`
  margin-top: 10px;
  display: flex;
  transition: all 0.7s ease-in-out;
  transform: ${({ index }: { index?: number }) =>
    `translateX(calc(-425px * ${index}))`};
`;
const CustomLink = styled(Link)`
  border: 1px solid #ebebeb;
  border-radius: 10px;
  margin: 0px 6.5px;
  cursor: pointer;
  color: #000;
  text-decoration: none;
  img {
    border-radius: 10px 10px 0 0;
    margin: 0 auto;
  }
`;
const Title = styled.div`
  width: 100%;
  height: 50px;
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  p {
    width: 270px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
  }
`;
const ButtonWrap = styled.div`
  display: flex;
  justify-content: center;
  margin: 10px auto;
  gap: 7px;
`;
const Button = styled.button`
  height: 5px;
  width: 5px;
  border-radius: 50%;
  border: none;
  padding: 0;
  cursor: pointer;
  background-color: ${({ isActive }: { isActive: boolean }) =>
    isActive ? "#f50057" : "lightgray"};
`;

export default Banner;

 

๋ฌดํ•œ ์Šฌ๋ผ์ด๋“œ + ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ํ•ด๋‹น ์Šฌ๋ผ์ด๋“œ๋กœ ์ด๋™ ํ•œ๋‹ค.

 


 

 

useEffect๊ฐ€ 2๋ฒˆ์‹คํ–‰๋ ๋•Œ

useEffect(() => {},[]) useEffect 2๋ฒˆ์งธ ์ธ์ž์— ๋นˆ ๋ฐฐ์—ด์„ ๋„ฃ์–ด์ฃผ๊ฒŒ ๋˜๋ฉด ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ฒ˜์Œ ๋ Œ๋”๋ง ๋ ๋•Œ๋งŒ ์‹คํ–‰๋œ๋‹ค. ๐Ÿ“Œ useEffect ์˜ค๋ฅ˜๋กœ 2๋ฒˆ ์‹คํ–‰ ๋ ๋•Œ ๐Ÿ“Œ src/index.js์—์„œ ํƒœ๊ทธ๋กœ ์ด ๊ฐ์‹ธ์ ธ์žˆ์œผ๋ฉด ๊ฐœ๋ฐœ๋ชจ๋“œ

eun-jii.tistory.com

 

728x90