React Native 抽屉导航只能运行一次。

huangapple go评论59阅读模式
英文:

react native Drawer navigation works only once

问题

I was using drawer navigation in react native and can slide the drawer from the left side of the screen and once I click inside the drawer element, the whole app freezes. scrolling doesn't work anymore drawer doesn't open, the only thing that works is the onPress method that I have on the message icon.

App.js code

const App = () => {
  return (
    <NavigationContainer>
      <MainNavigation />
    </NavigationContainer>
  );
};

Navigation code

const Stack = createStackNavigator();
const Drawer = createDrawerNavigator();

const MainMenuNavigation = () => {
  return (
    <Drawer.Navigator screenOptions={{header: () => null}}>
      <Drawer.Screen name={Routes.Home} component={Home} />
      <Drawer.Screen name={Routes.Profile} component={Profile} />
    </Drawer.Navigator>
  );
};

const mainNavigation = () => {
  return (
    <Stack.Navigator
      initialRouteName={Routes.Home}
      screenOptions={{header: () => null, headerShown: false}}>
      <Stack.Screen name={'Drawer'} component={MainMenuNavigation} />
    </Stack.Navigator>
  );
};

Home page code

import React, { useState } from 'react';
import {
  SafeAreaView,
  Text,
  View,
  Pressable,
  FlatList,
  StatusBar,
} from 'react-native';
import Title from '../../components/title/Title';
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome';
import { faEnvelope } from '@fortawesome/free-regular-svg-icons';
import style from './style';
import globalStyle from '../../Zassets/styles/main';
import UserStory from '../../components/UserStory/UserStory';
import UserPost from '../../components/userPosts/userPost';
import { Routes } from '../../navigation/routes';

const Home = ({ navigation }) => {
  const data = [
    {
      firstName: 'joseph',
      id: 1,
    },
    {
      firstName: 'Angel',
      id: 2,
    },
    // ... (other data entries)
  ];

  // ... (other variables and functions)

  return (
    <SafeAreaView style={globalStyle.backgroundWhite}>
      <StatusBar backgroundColor={'#fff'} barStyle={'dark-content'} />
      <FlatList
        // ... (FlatList props)
      />
    </SafeAreaView>
  );
};

export default Home;

video for more clarity

英文:

I was using drawer navigation in react native and can slide the drawer from the left side of the screen and once I click inside the drawer element, the whole app freezes. scrolling doesn't work anymore drawer doesn't open, the only thing that works is the onPress method that I have on the message icon.

App.js code

const App = () =&gt; {
  return (
    &lt;NavigationContainer&gt;
      &lt;MainNavigation /&gt;
    &lt;/NavigationContainer&gt;
  );
};

Navigation code

const Stack = createStackNavigator();
const Drawer = createDrawerNavigator();

const MainMenuNavigation = () =&gt; {
  return (
    &lt;Drawer.Navigator screenOptions={{header: () =&gt; null}}&gt;
      &lt;Drawer.Screen name={Routes.Home} component={Home} /&gt;
      &lt;Drawer.Screen name={Routes.Profile} component={Profile} /&gt;
    &lt;/Drawer.Navigator&gt;
  );
};

const mainNavigation = () =&gt; {
  return (
    &lt;Stack.Navigator
      initialRouteName={Routes.Home}
      screenOptions={{header: () =&gt; null, headerShown: false}}&gt;
      &lt;Stack.Screen name={&#39;Drawer&#39;} component={MainMenuNavigation} /&gt;
    &lt;/Stack.Navigator&gt;
  );
};

Home page code

import React, {useState} from &#39;react&#39;;
import {
  SafeAreaView,
  Text,
  View,
  Pressable,
  FlatList,
  StatusBar,
} from &#39;react-native&#39;;
import Title from &#39;../../components/title/Title&#39;;
import {FontAwesomeIcon} from &#39;@fortawesome/react-native-fontawesome&#39;;
import {faEnvelope} from &#39;@fortawesome/free-regular-svg-icons&#39;;
import style from &#39;./style&#39;;
import globalStyle from &#39;../../Zassets/styles/main&#39;;
import UserStory from &#39;../../components/UserStory/UserStory&#39;;
import UserPost from &#39;../../components/userPosts/userPost&#39;;
import { Routes } from &#39;../../navigation/routes&#39;;

const Home = ({navigation}) =&gt; {
  const data = [
    {
      firstName: &#39;joseph&#39;,
      id: 1,
    },
    {
      firstName: &#39;Angel&#39;,
      id: 2,
    },
    {
      firstName: &#39;White&#39;,
      id: 3,
    },
    {
      firstName: &#39;Olivier&#39;,
      id: 4,
    },
    {
      firstName: &#39;john&#39;,
      id: 5,
    },
    {
      firstName: &#39;nick&#39;,
      id: 6,
    },
    {
      firstName: &#39;black&#39;,
      id: 7,
    },
    {
      firstName: &#39;Ann&#39;,
      id: 8,
    },
    {
      firstName: &#39;Mary&#39;,
      id: 9,
    },
  ];
  const pageSize = 4;
  const [pageNumber, setPageNumer] = useState(1);
  const [loading, setLoading] = useState(false);
  const [renderedData, setRenderedData] = useState(data.slice(0, pageSize));

  const posts = [
    {
      firstName: &#39;Allison&#39;,
      lastName: &#39;becker&#39;,
      location: &#39;Sukabumi, Jawa Barat&#39;,
      likes: 1200,
      comments: 24,
      bookmarks: 55,
      id: 1,
    },
    {
      firstName: &#39;Jennifer&#39;,
      lastName: &#39;Wilkson&#39;,
      location: &#39;Pondok leugnsir, Jawa Barat&#39;,
      likes: 1527,
      comments: 15,
      bookmarks: 25,
      id: 2,
    },
    {
      firstName: &#39;Adam&#39;,
      lastName: &#39;spera&#39;,
      location: &#39;Boston, Massachusetts&#39;,
      likes: 125,
      comments: 6,
      bookmarks: 18,
      id: 3,
    },
    {
      firstName: &#39;john&#39;,
      lastName: &#39;doe&#39;,
      location: &#39;NY, US&#39;,
      likes: 1525,
      comments: 63,
      bookmarks: 73,
      id: 4,
    },
    {
      firstName: &#39;mary&#39;,
      lastName: &#39;Ann&#39;,
      location: &#39;Paris, France&#39;,
      likes: 244,
      comments: 23,
      bookmarks: 13,
      id: 5,
    },
  ];
  const postsPageSize = 2;
  const [pageNumberPosts, setpageNumberPosts] = useState(1);
  const [loadingPosts, setloadingPosts] = useState(false);
  const [renderedDataPosts, setrenderedDataPosts] = useState(
    posts.slice(0, postsPageSize),
  );

  const pagination = (data, pageNumber, pageSize, posts = false) =&gt; {
    let startIndex = (pageNumber - 1) * pageSize;
    if (startIndex &gt; data.length) {
      return [];
    }

    if (!posts) {
      setPageNumer(pageNumber);
    } else {
      setpageNumberPosts(pageNumber);
    }
    return data.slice(startIndex, startIndex + pageSize);
  };
  
  return (
    &lt;SafeAreaView style={globalStyle.backgroundWhite}&gt;
      &lt;StatusBar backgroundColor={&#39;#fff&#39;} barStyle={&#39;dark-content&#39;} /&gt;
      &lt;FlatList
        ListHeaderComponent={
          &lt;&gt;
            &lt;View style={style.header}&gt;
              &lt;Title title={&quot;Let&#39;s Explore&quot;} /&gt;
              &lt;Pressable
                onPress={() =&gt; {
                  navigation.navigate(Routes.Profile);
                }}
                style={style.messageIcon}&gt;
                &lt;FontAwesomeIcon
                  icon={faEnvelope}
                  color={&#39;#CACDDE&#39;}
                  size={20}
                /&gt;
                &lt;View style={style.messageIconView}&gt;
                  &lt;Text style={style.messageIconText}&gt;2&lt;/Text&gt;
                &lt;/View&gt;
              &lt;/Pressable&gt;
            &lt;/View&gt;
            &lt;View style={style.userStoryContainer}&gt;
              &lt;FlatList
                onMomentumScrollBegin={() =&gt; setLoading(false)}
                onEndReachedThreshold={0.5}
                keyExtractor={item =&gt; item.id.toString()}
                onEndReached={() =&gt; {
                  if (!loading) {
                    setLoading(true);
                    setRenderedData(prev =&gt; [
                      ...prev,
                      ...pagination(data, pageNumber + 1, pageSize),
                    ]);
                    setLoading(false);
                  }
                }}
                showsHorizontalScrollIndicator={false}
                horizontal={true}
                data={renderedData}
                renderItem={({item}) =&gt; (
                  &lt;UserStory firstName={item.firstName} /&gt;
                )}
              /&gt;
            &lt;/View&gt;
          &lt;/&gt;
        }
        onMomentumScrollBegin={() =&gt; setloadingPosts(false)}
        onEndReachedThreshold={0.5}
        keyExtractor={item =&gt; item.id.toString() + &#39; post&#39;}
        onEndReached={() =&gt; {
          if (!loadingPosts) {
            setloadingPosts(true);
            setrenderedDataPosts(prev =&gt; [
              ...prev,
              ...pagination(posts, pageNumberPosts + 1, postsPageSize, true),
            ]);
            setloadingPosts(false);
          }
        }}
        showsVerticalScrollIndicator={false}
        data={renderedDataPosts}
        renderItem={({item}) =&gt; (
          &lt;View style={style.userPostContainer}&gt;
            &lt;UserPost
              firstName={item.firstName}
              lastName={item.lastName}
              comments={item.comments}
              likes={item.likes}
              bookmarks={item.bookmarks}
              location={item.location}
            /&gt;
          &lt;/View&gt;
        )}
      /&gt;
    &lt;/SafeAreaView&gt;
  );
};

export default Home;

video for more clarity

答案1

得分: 1

降级 react-native-reanimated 到版本 3.1.0 对我有效。

英文:

downgrade react-native-reanimated to version 3.1.0 works for me.

huangapple
  • 本文由 发表于 2023年6月5日 22:35:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定