Skip to content

Keeps rerendering ListView tab #226

Open

Description

@tomjvk

Please also see this stackoverflow question: https://stackoverflow.com/questions/70857346/shared-navigation-infinite-loop-react-native.

The ListView in the title references here to Home component.

const HomeTab = () =>  {
    const Stack = createSharedElementStackNavigator();
    return (
            <Stack.Navigator initialRouteName="Home">
                <Stack.Screen key='home' name='Home' component={Home} />
                <Stack.Screen name='Detail' key='detail' component={Detail} sharedElements=
                    {(route, otherRoute, showing) => {
                        //Code in here is never executed
                        return [route.params.id];
                     }}
                />
                <Stack.Screen name='t3' key='t3' component={T3}  /> 
            </Stack.Navigator>
    );
};
const Home = ({ navigation }) => {
    const [APIHook, setAPIHook] = useState(null);
    
    useEffect(() => {
        fetchAPIData();
    }, []);

    const fetchAPIData = async () => {
        //Gets the data
    }

    if (!APIData){
        return (<Text>Loading</Text>);
    }

    return (
        <FlatList data={timeline} renderItem={({item}) => renderDetail(navigation, item)} keyExtractor={(item) => item.id.toString()}/>
    );
};

export default Home;
export default renderDetail = (navigation, item) => (
    ...
    <View onPress={() => navigation.push("Detail", item)}>
        <SharedElement id = {props.id}>
            <Image width={width} source={{uri: props.source}} />
         </SharedElement>
    </View>
    ...
);
const Detail= ({ route, navigation }) => {
    let detail = route.params;
    return (
        ...
        <SharedElement id = {props.id}>
            <Image width={width} source={{uri: props.source}} />
         </SharedElement>
         ...
    )
};
export default Detail;

When clicking om image on Home program gets stuck in infinite loop: first it renders the HomeTab component again -> Home component -> Detail (renderDetail is not rendered again in this loop).

Activity

  1. tomjvk commented on Apr 1, 2022

    @tomjvk
    Author

    Moving const Stack = createSharedElementStackNavigator(); outside the scope fixed the issue:

    const HomeTab = () =>  {
        const Stack = createSharedElementStackNavigator();
    

    TO

    const Stack = createSharedElementStackNavigator();
    const HomeTab = () =>  {
    
  2. thespacemanatee commented on May 8, 2022

    @thespacemanatee

    Thanks! We actually faced the same problem too, did not realise this was ever a problem until createSharedElementStackNavigator, and we decided to migrate all our other navigators to be created at the top-level as well to possibly save some re-renders.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions