Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

81
Visualizações
Multiple column carousel react

I am trying to make a multi column carousel in React (4 columns) that works responsively (it still scrolls by 4 when on tablet or desktop). When you click the arrows, I want it to slide by 4 so that the next 4 items are shown. At the moment, my clickCount is not updating in time for me to set the scrollTo to the correct thing. Also, I've been having issues when resizing my screen (sometimes it will skip an item or go too far). Btw, each item has a width of 25%.

This is what I have so far below -

const [clickCount, setClickCount] = useState(0)

const scrollSlide = (direction: 'next' | 'prev') => {
    const width = mainWrap.current?.clientWidth
    if (direction === 'next') {
        setClickCount(clickCount + 1)
        scrollTo = width * clickCount
    } else {
        setClickCount(prevState => prevState - 1)
        scrollTo = scrollTo - width
    }
    containerRef.current?.scrollTo({
        behaviour: 'smooth',
        left: scrollTo
    })
}

return (
    <div ref={mainWrap}>
        <button onClick={() => scrollSllides('prev')}>Prev</button>
        <button onClick={() => scrollSllides('next')}>Next</button>
        <div ref={containerRef}>
            {items?.map((item, index) => (
                <ItemComponent
                    key={index}
                    title={item.title}
                    image={item.image}
                />
            ))}
        </div>
    </div>
)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I just defined this slider, based on the code which you provide: https://codesandbox.io/s/epic-feynman-jvt1q?file=/src/styles.css

const Slider = ({ items }) => {
  const [clickCount, setClickCount] = React.useState(0);
  const mainWrap = React.useRef();
  const containerRef = React.useRef();

  const scrollSllides = (direction) => {
    const width = mainWrap.current?.clientWidth;
    let scrollTo;

    const diff = direction === "next" ? 1 : -1;
    const newValue = (clickCount + diff) % (items.length / 4);
    setClickCount(newValue);
    scrollTo = width * newValue;

    containerRef.current?.scrollTo({
      behavior: "smooth",
      left: scrollTo
    });
  };

  return (
    <div ref={mainWrap}>
      <button onClick={() => scrollSllides("prev")}>Prev</button>
      <button onClick={() => scrollSllides("next")}>Next</button>
      <div className="Slider" ref={containerRef}>
        {items?.map((item, index) => (
          <ItemComponent key={index} title={item.title} image={item.image} />
        ))}
      </div>
    </div>
  );
};

With next CSS:

.Slider {
  width: 100%;
  height: 100px;
  overflow: scroll;
  white-space: nowrap;
}

.ItemComponent {
  display: inline-block;
  width: 25%;
  height: 100px;
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda