Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keeps rerendering ListView tab #226

Open
Tomvkgames opened this issue Jan 29, 2022 · 2 comments
Open

Keeps rerendering ListView tab #226

Tomvkgames opened this issue Jan 29, 2022 · 2 comments

Comments

@Tomvkgames
Copy link

Tomvkgames commented Jan 29, 2022

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

@Tomvkgames
Copy link
Author

Tomvkgames commented Apr 1, 2022

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

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

TO

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

@thespacemanatee
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants