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

231
Visualizações
How to keep position in ScrollView? React Native

I want to make horizontal ScrollView with 6 TouchableOpacity element's; onPress they would change some redux state, and then, chenge they own style, how to force list KEEP position on rerender? Now if I press after some scrolling it goes back to the start

I want this look something like category choosing in Play Market, but without centering, it just need keeps the position

here how it looks

    <ScrollView 
style={{ height: 50, width: '100%' }} 
contentContainerStyle={{ alignItems: 'center' }} 
horizontal={true} 
showsHorizontalScrollIndicator={false}
>
  <TouchableOpacity
    style={categoryNumber === 0 ? styles.BtnActive : styles.Btn}
    onPress={() => dispatch(setCategoryNumber(0))}>
    <Text style={categoryNumber === 0 ? styles.BtnTextActive : styles.BtnText}>ITEM_NAME 1</Text>
  </TouchableOpacity>
  <TouchableOpacity
    style={categoryNumber === 1 ? styles.BtnActive : styles.Btn}
    onPress={() => dispatch(setCategoryNumber(1))}>
    <Text style={categoryNumber === 1 ? styles.BtnTextActive : styles.BtnText}>ITEM_NAME 2</Text>
  </TouchableOpacity>
  <TouchableOpacity
    style={categoryNumber === 2 ? styles.BtnActive : styles.Btn}
    onPress={() => dispatch(setCategoryNumber(2))}>
    <Text style={categoryNumber === 2 ? styles.BtnTextActive : styles.BtnText}>ITEM_NAME 3</Text>
  </TouchableOpacity>
  <TouchableOpacity
    style={categoryNumber === 3 ? styles.BtnActive : styles.Btn}
    onPress={() => dispatch(setCategoryNumber(3))}>
    <Text style={categoryNumber === 3 ? styles.BtnTextActive : styles.BtnText}>ITEM_NAME 4</Text>
  </TouchableOpacity>
    <TouchableOpacity
    style={categoryNumber === 4 ? styles.BtnActive : styles.Btn}
    onPress={() => {dispatch(setCategoryNumber(4))}}>
    <Text style={categoryNumber === 4 ? styles.BtnTextActive : styles.BtnText}>ITEM_NAME 5</Text>
  </TouchableOpacity>
    <TouchableOpacity
    style={categoryNumber === 5 ? styles.BtnActive : styles.Btn}
    onPress={() => {dispatch(setCategoryNumber(5))}}>
    <Text style={categoryNumber === 5 ? styles.BtnTextActive : styles.BtnText}>ITEM_NAME 6</Text>
  </TouchableOpacity>
</ScrollView>


const styles = StyleSheet.create({
  Btn: {
    height: 40,
    backgroundColor: '#F1F5FE',
    paddingHorizontal: 10,
    justifyContent: 'center',
    borderRadius: 12,
    marginHorizontal: 4
  },
  BtnText: {
    color: '#000'
  },
  BtnActive: {
    height: 40,
    backgroundColor: '#336FEE',
    paddingHorizontal: 10,
    justifyContent: 'center',
    borderRadius: 12,
    marginHorizontal: 4
  },
  BtnTextActive: {
    color: '#fff'
  }
})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Add unique keys to the components of the ScrollView

Here is why you need keys

Example:

  <TouchableOpacity
     key={'some-unique-id'}
     style={categoryNumber === 0 ? styles.BtnActive : styles.Btn}
     onPress={() => dispatch(setCategoryNumber(0))}>
    <Text style={categoryNumber === 0 ? styles.BtnTextActive : styles.BtnText}>
     ITEM_NAME 1
    </Text>
  </TouchableOpacity>

Tip: Try to use loops instead of hard writing things in ScrollView

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