Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!