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

355
Views
How do you center a flat list with flex=1 items and not shrink the content while keeping a full width?

I am trying to set up a flat list with items having the following properties.

  • it has flex: 1
  • it has no specified width (the height may be set in the content for images)
  • it is maxWidth set
  • the width cannot be forced to 100% because the intent is to have the flat list support multiple columns
  • minWidth cannot be set since it will prevent shrinking of content if it is too large to fit with number of columns.

The FlatList is the top level component of a Screen so it will do the collapsing header in iOS. To which

  • it takes up the full width
  • it has some padding
  • the scroll bar should be flushed right (so you can't do padding on the FlatList itself

So what I have is something like

const data = Array.from({length: 5}, (v,k) => ({v,k}));
function renderItem({item, index}) {
  return (<View style={{
    flex:1, 
    maxWidth: 100, 
    borderWidth: 1, 
    backgroundColor: 'red',
    // alignSelf: "center" (also shrinks)
  }}>
    <Text>{item.k}</Text>
  </View>)
}

return (<FlatList
  style={StyleSheet.absoluteFill}
  contentContainerStyle={{
    padding: 16,
    // alignSelf: center (shrinks content)
  }}
  renderItem={renderItem}
  data={data}
/>)

I'd also like to avoid onLayout to prevent too much rerenders

https://snack.expo.dev/@trajano/flatlistneedscentering

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There's another prop called columnWrapperStyle which does what I needed.

    <FlatList
      columnWrapperStyle={{
        justifyContent: 'center',
      }}
      contentContainerStyle={{
        paddingVertical: 16,
      }}
      initialNumToRender={2}
      renderItem={renderItem}
      data={data}
      numColumns={numColumns} //can be set to 3 to force shrinking
      ItemSeparatorComponent={() => <View style={{ height: 16 }} />}
      keyExtractor={(item) => item.k.toString()}
    />
  );

Note that NativeBase does not expose this as a utility prop.

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!