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

186
Visualizações
Tab Component - Horizontally Scrollable tab buttons re rendering on click

Component Details: This is a tab component where the tab buttons are horizontally scrollable

Problem: When i scroll horizontally and click on a tab button. The horizontal scroll is reset. Maybe The entire tab container is re-rendering due to the setState IDK.

Expecting: I want to be able to click on a tab button and not update the horizontal scroll position of the button container.

Current Code:

function Tabs(props) {

const [tabState, setTabState] = useState(0);
const [menuData, setMenuData] = useState('')


const TabContainer = styled.div`
    width: 100%;
    display: flex;
    flex-wrap: no-wrap;
    flex-direction: row;
    margin: 0 0 20px 0;
    padding: 20px;
    align-items: flex-start;
    justify-content: flex-start;
    background-color: #fff;
    overflow-x: auto;
   
  const handleTabClick = (index) => {
       setTabState(index);}return(
    <>
       

        <TabParent>
            <TabContainer>
            {props.data && props.data.dataDeets.map((dataDeets,catIndex) => {
                return (
                
                    <TabBtn key={catIndex} onClick={() => handleTabClick(catIndex) } active={tabState === catIndex ? true : false}>
                        {menu.category}
                    </TabBtn>

                )
                })
            }

            </TabContainer>

  
        </TabParent>


        {props.data && props.data.menu.map((menu, catIndex) => {
            return (
                <TabCont key={catIndex} active={tabState === catIndex ? true : false}> 
                <small>
                    {data.categoryDesc}
                </small>
                {data.items.map((items, itemIndex) => {
                    return (
                        <div key={itemIndex} onClick={event => props.openDetails(catIndex, itemIndex, event)}>
                            <ItemCard key={itemIndex} title={items.title} imgurl={items.imgurl} desc={items.desc} price={items.price}  />
                        </div>
                    )
                    })
                }

                </TabCont>
                
            )
        })
        }

        

    </>
);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

My Rookie mistake: Hopefully this will helps other rookies :)

useState caused the entire component to re-render

My Solution Move the out as a separate component. This enables To maintain its own state.

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