TransWikia.com

Flatlist maintainVisibleContentPosition causes instant crash on iOs

Stack Overflow Asked by Ryan Pergent on November 21, 2020

I have an app with only this component:

function TestComp() {
    return (
        <FlatList
            keyExtractor={(item) => String(item)}
            data={[0, 1, 2, 3]}
            renderItem={({ item }) => (
                <View>
                    <Text>{item}</Text>
                </View>
            )}
            maintainVisibleContentPosition     <--- Causes crash
        />
    );
}

The program crashes immegiately, throwing the error:
Exception thrown while executing UI block: - [__NSCFBoolean objectForKeyedSubscript:]: unrecognized selector sent to instance 0x11bfcae48.

The crash disappears when I remove the maintainVisibleContentPosition props (but I need it).

I am on react 16.11.0, using expo sdk 38.0.0.

Any idea why this is happening?

enter image description here

2 Answers

This happens because React is expecting you to define the minimum visible position even if it's zero. So instead of just putting maintainVisibleContentPosition, try using

maintainVisibleContentPosition={{
     minIndexForVisible: 0,
  }}

Correct answer by Softasu on November 21, 2020

maintainVisibleContentPosition here is official docs

the App is crashing due because maintainVisibleContentPosition is aspecting a object

try this

maintainVisibleContentPosition={{
     minIndexForVisible: 0,
  }}

here is demo: https://snack.expo.io/@nomi9995/flastlist-fix-crash

import * as React from 'react';
import { FlatList, Text, View } from 'react-native';

export default function App() {
  return (
        <FlatList
            keyExtractor={(item) => String(item)}
            data={[0, 1, 2, 3]}
            renderItem={({ item }) => (
                <View>
                    <Text>{item}</Text>
                </View>
            )}
            maintainVisibleContentPosition={{
              minIndexForVisible: 0,
            }}
        />
    );
}

Answered by Muhammad Numan on November 21, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP