asyncstorage在react native的构建模式下无法工作

yh2wf1be  于 2023-01-14  发布在  React
关注(0)|答案(2)|浏览(157)

在调试模式asyncstorage工作完美的罚款,但当建立apk与此命令

gradlew assembleRelease -x bundleReleaseJsAndAssetsAPK构建完美,但我想打开它显示我的手机中的错误appstop

关于这方面的任何帮助都将非常感激

9fkzdhlc

9fkzdhlc1#

试试这个...

const [loading, setLoading] = useState(true);
  const [isFirstTimeLoad, setIsFirstTimeLoad] = useState(false);
 
 const checkForFirstTimeLoaded = async () => {
    const result = await AsyncStorage.getItem('isFirstTimeOpen');
    console.log('result:', result);
    if (result == null) {
      setIsFirstTimeLoad(true);
      setLoading(false);
    } else {
      setIsFirstTimeLoad(false);
      setLoading(false);
    }
  };
  
   if (loading)
    return (
      <View style={{flex:1,justifyContent:'center',alignItems:'center'}}>
        <ActivityIndicator  size={'large'} color={'black'}/>
      </View>
    );
    
    
    if (isFirstTimeLoad)
    return (
      <NavigationContainer>
        <Stack.Navigator
          initialRouteName="OnboardingScreen"
          screenOptions={{
            headerShown: false,
            // header: () => <MainHeader />,
          }}>
          <Stack.Screen name="OnboardingScreen" component={OnBoardingScreen} />
          <Stack.Screen name="login" component={Login} />
          <Stack.Screen name="home" component={Home} />
          <Stack.Screen name="register" component={Register} />
          <Stack.Screen name="mobileverify" component={MobileVerify} />
          <Stack.Screen name="listscreen" component={ListData} />
        </Stack.Navigator>
      </NavigationContainer>
    );
    
    if (!isFirstTimeLoad) return <Login />;
5ssjco0h

5ssjco0h2#

Try this.
import AsyncStorage from '@react-native-async-storage/async-storage';

        export default function App() 
        {
         const [aldreadyLaunched, setaldreadyLaunched] = useState(true)
        useEffect(() => {
          AsyncStorage.getItem("alreadyLaunched").then((value) => {
            if (value == "false") {
              let parsedValue = JSON.parse(value)
              setaldreadyLaunched(parsedValue)
            }
        
          })  
        },[])
    return (<>
        display introductory screen when the already launched state is true and when the false display login or directly main screen
    </>)
        }

相关问题