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

77
Visualizações
Horizontal Infinite Scroll with API request

I am trying to make an infinite horizontal scroll feature, where when the user reaches the rightmost or the last item of the list, an API request will be triggered and the user can scroll more.

enter image description here

I am already able to somewhat achieve this using the onTouchEnd event handler. However, upon getting the next items, it adds it and then scrolls back to the first item. I guess that has something to do with re-rendering in react?

Is there perhaps some better way of handling this? Or at the very least, go back to the previous position the user scrolled to. I've searched a lot of libraries but they don't seem to achieve what I need.

Here is what I have done so far:

import React from 'react';
import { Box, Avatar } from '@mui/material';
import {
  useDispatch,
  useSelector,
} from '../../../../redux/store/configureStore';
import {
  nextPage,
  fetchAsyncMerchants,
} from '../../redux/slices/deals/Merchants';

const MerchantList = () => {
  const dispatch = useDispatch();
  const limit = 10;
  const { skip, merchants } = useSelector((state) => state.merchants);
  const boxWidthRef = React.useRef();

  React.useEffect(() => {
    dispatch(fetchAsyncMerchants({ skip, limit }));
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [skip]);

  return (
    <Box sx={{ mt: '12px', mb: '16px' }}>
      {merchants && (
        <Box
          ref={boxWidthRef}
          onTouchEnd={(event) => {
            dispatch(nextPage(limit)); //updates the skip counter to rerun useEffect
          }}
          onTouchStart={(event) => {
            console.log(event.touches[0]);
          }}
          sx={{
            minHeight: '120px',
            boxShadow: 5,
            display: 'flex',
            justifyContent: 'space-between',
            borderRadius: '16px',
            overflow: 'auto',
            p: '0 5px',
          }}
        >
          {merchants.map((merchant) => {
            return (
              <Box
                id="card-btn"
                key={merchant.id}
                sx={{
                  display: 'flex',
                  flexDirection: 'column',
                  minWidth: '100px',
                }}
              >
                <Avatar
                  sx={{
                    height: '40px',
                    width: '40px',
                    mb: '4px',
                  }}
                  src={merchant.logo}
                />
              </Box>
            );
          })}
        </Box>
      )}
    </Box>
  );
};

export default MerchantList;

about 4 years ago · Juan Pablo Isaza
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