Skip to content

Instantly share code, notes, and snippets.

@Stringsaeed
Last active March 2, 2022 16:00
Show Gist options
  • Select an option

  • Save Stringsaeed/eb1ba3450417d5ef2ea4926b48cc1fb1 to your computer and use it in GitHub Desktop.

Select an option

Save Stringsaeed/eb1ba3450417d5ef2ea4926b48cc1fb1 to your computer and use it in GitHub Desktop.

Revisions

  1. Stringsaeed revised this gist Mar 2, 2022. 1 changed file with 52 additions and 2 deletions.
    54 changes: 52 additions & 2 deletions useGetFirstTime.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,54 @@
    ### useGetFirstTime

    #### Description
    React Hook used for when user first time opens the app so if you want to show tutorial modal or load only onboarding stack in react-navigation & react-native
    #### Description:
    React Hook used for when user first time opens the app so if you want to show tutorial modal or load only onboarding stack in react-navigation & react-native

    #### Usage:
    ```tsx
    import React, { useEffect } from "react";
    import { NavigationContainer } from "@react-navigation/native";
    import { createStackNavigator } from "@react-navigation/stack";

    const OnBoardingStack = createStackNavigator();
    const AppStack = createStackNavigation();

    const OnboardingNavigator = ({ setFirstTime }) => {
    useEffect(() => {
    setFirstTime();
    }, [setFirstTime]);

    return (
    <OnBoardingStack.Navigator>
    <OnBoardingStack.Screen
    name="onBoarding1"
    component={OnBoardingOneScreen}
    />
    <OnBoardingStack.Screen
    name="onBoarding2"
    component={OnBoardingTwoScreen}
    />
    </OnBoardingStack.Navigator>
    );
    };

    const AppNavigator = () => {
    return (
    <AppStack.Navigator>
    <AppStack.Screen name="App1" component={AppOneScreen} />
    <AppStack.Screen name="App2" component={AppTwoScreen} />
    </AppStack.Navigator>
    );
    };

    const App = () => {
    const [firstTime, setFirstTime] = useGetFirstTime();

    return (
    <NavigationContainer>
    {firstTime ? <OnboardingNavigator /> : <AppNavigator />}
    </NavigationContainer>
    );
    };

    export default App;
    ```
  2. Stringsaeed revised this gist Mar 2, 2022. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion useGetFirstTime.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    ### useGetFirstTime

    #react-native #react
    #### Description
    React Hook used for when user first time opens the app so if you want to show tutorial modal or load only onboarding stack in react-navigation & react-native
  3. Stringsaeed revised this gist Mar 2, 2022. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion useGetFirstTime.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,3 @@
    ‎‎​
    ### useGetFirstTime

    #react-native #react
  4. Stringsaeed renamed this gist Mar 2, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. Stringsaeed renamed this gist Mar 2, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. Stringsaeed revised this gist Mar 2, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Introduction.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ‎‎​
  7. Stringsaeed revised this gist Mar 2, 2022. No changes.
  8. Stringsaeed revised this gist Mar 2, 2022. No changes.
  9. Stringsaeed revised this gist Mar 2, 2022. No changes.
  10. Stringsaeed revised this gist Mar 2, 2022. No changes.
  11. Stringsaeed revised this gist Mar 2, 2022. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions useGetFirstTime.js
    Original file line number Diff line number Diff line change
    @@ -1,28 +1,28 @@
    import { useCallback, useLayoutEffect, useState } from "react";
    import { useAsyncStorage } from "@react-native-async-storage/async-storage";

    const useGetFirstTimeTipping = () => {
    const [firstTimeTipping, setFirstTimeTipping] = useState(false);
    const { getItem, setItem } = useAsyncStorage("TippingModalVisited");
    const useGetFirstTime = () => {
    const [firstTime, setFirstTime] = useState(false);
    const { getItem, setItem } = useAsyncStorage("FirstTime");
    const readItemFromStorage = useCallback(async () => {
    const item = await getItem();
    setFirstTimeTipping(!item);
    setFirstTime(!item);
    }, [getItem]);

    const setTippingModalVisited = useCallback(async () => {
    setFirstTimeTipping(true);
    const setModalVisited = useCallback(async () => {
    setFirstTime(true);
    await setItem(true);
    }, [setItem]);

    // using useLayoutEffect to avoid re-rendering
    // so it get value before view mounts
    // of course the modal won't mount before
    // of course, the modal won't mount before
    // we check for layout height or the position.
    useLayoutEffect(() => {
    readItemFromStorage();
    }, [readItemFromStorage]);

    return [firstTimeTipping, setTippingModalVisited];
    return [firstTime, setModalVisited];
    };

    export default useGetFirstTimeTipping;
    export default useGetFirstTime;
  12. Stringsaeed created this gist Mar 2, 2022.
    28 changes: 28 additions & 0 deletions useGetFirstTime.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    import { useCallback, useLayoutEffect, useState } from "react";
    import { useAsyncStorage } from "@react-native-async-storage/async-storage";

    const useGetFirstTimeTipping = () => {
    const [firstTimeTipping, setFirstTimeTipping] = useState(false);
    const { getItem, setItem } = useAsyncStorage("TippingModalVisited");
    const readItemFromStorage = useCallback(async () => {
    const item = await getItem();
    setFirstTimeTipping(!item);
    }, [getItem]);

    const setTippingModalVisited = useCallback(async () => {
    setFirstTimeTipping(true);
    await setItem(true);
    }, [setItem]);

    // using useLayoutEffect to avoid re-rendering
    // so it get value before view mounts
    // of course the modal won't mount before
    // we check for layout height or the position.
    useLayoutEffect(() => {
    readItemFromStorage();
    }, [readItemFromStorage]);

    return [firstTimeTipping, setTippingModalVisited];
    };

    export default useGetFirstTimeTipping;