react-navigation-shared-element icon indicating copy to clipboard operation
react-navigation-shared-element copied to clipboard

Keeps rerendering ListView tab

Open tomjvk opened this issue 3 years ago • 2 comments

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).

tomjvk avatar Jan 29 '22 15:01 tomjvk

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

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

TO

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

tomjvk avatar Apr 01 '22 19:04 tomjvk

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.

thespacemanatee avatar May 08 '22 12:05 thespacemanatee