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

fix(boilerplate): fix deep link not working with navigation persist #2679

Merged
merged 1 commit into from
May 29, 2024

Conversation

vanenshi
Copy link
Contributor

@vanenshi vanenshi commented May 5, 2024

Please verify the following:

  • yarn test jest tests pass with new tests, if relevant
  • yarn lint eslint checks pass with new code, if relevant
  • yarn format:check prettier checks pass with new code, if relevant
  • README.md (or relevant documentation) has been updated with your changes

Describe your PR

On the dev env, if you try to start the app from a deep link because the navigation persists, it will get ignored by the navigation

@vanenshi vanenshi changed the title Fix deep link not working on dev mode fix(boilerplate): fix deep link not working with navigation persist May 5, 2024
@frankcalise
Copy link
Contributor

@vanenshi Thanks for the contribution! Can you supply steps to reproduce / test this change for us so we can confirm the fix?

@vanenshi
Copy link
Contributor Author

vanenshi commented May 7, 2024

@vanenshi Thanks for the contribution! Can you supply steps to reproduce / test this change for us so we can confirm the fix?

Hi @frankcalise
Sure, to test the PR, you need to start the application from a deep link

  1. build and run the boilerplate
  2. enable the navigation to persist
  3. kill the app, and click on a deep link in the simulator (for example helloworld:://community)

Expected Behavior

open the app and go to the community screen

Actual behavior

app stays on the persisted navigation status (last page that was open)

Note

I picked up the fix from the react-navigation website, here is their example code, pay attention to the const initialUrl = await Linking.getInitialURL(); line
https://reactnavigation.org/docs/state-persistence/#usage

import * as React from 'react';
import { Linking, Platform } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { NavigationContainer } from '@react-navigation/native';

const PERSISTENCE_KEY = 'NAVIGATION_STATE_V1';

export default function App() {
  const [isReady, setIsReady] = React.useState(Platform.OS === 'web'); // Don't persist state on web since it's based on URL
  const [initialState, setInitialState] = React.useState();

  React.useEffect(() => {
    const restoreState = async () => {
      try {
        const initialUrl = await Linking.getInitialURL();

        if (initialUrl == null) {
          // Only restore state if there's no deep link
          const savedStateString = await AsyncStorage.getItem(PERSISTENCE_KEY);
          const state = savedStateString
            ? JSON.parse(savedStateString)
            : undefined;

          if (state !== undefined) {
            setInitialState(state);
          }
        }
      } finally {
        setIsReady(true);
      }
    };

    if (!isReady) {
      restoreState();
    }
  }, [isReady]);

  if (!isReady) {
    return null;
  }

  return (
    <NavigationContainer
      initialState={initialState}
      onStateChange={(state) =>
        AsyncStorage.setItem(PERSISTENCE_KEY, JSON.stringify(state))
      }
    >
      {/* ... */}
    </NavigationContainer>
  );
}

@mustafahsz
Copy link

any update on this? we have the same problem

@frankcalise
Copy link
Contributor

@mustafahsz later this week probably, but feel free to just incorporate the code into your project

Copy link
Contributor

@frankcalise frankcalise left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution and the provided documentation, left one question for you

boilerplate/app/navigators/navigationUtilities.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@frankcalise frankcalise left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@frankcalise frankcalise merged commit a0cd8ce into infinitered:master May 29, 2024
1 check passed
@vanenshi vanenshi deleted the patch-2 branch May 29, 2024 14:26
infinitered-circleci pushed a commit that referenced this pull request May 29, 2024
# [9.7.0](v9.6.3...v9.7.0) (2024-05-29)

### Bug Fixes

* **boilerplate:** fix deep link not working with navigation persist ([#2679](#2679) by [@vanenshi](https://github.com/vanenshi)) ([a0cd8ce](a0cd8ce))
* **cli:** better support for yarn 2+ ([#2684](#2684) by [@frankcalise](https://github.com/frankcalise) and [@morganick](https://github.com/morganick)) ([e38a02c](e38a02c))
* **metro:** Add support for cjs extension in metro.config.js ([#2689](#2689) by [@jamonholmgren](https://github.com/jamonholmgren)) ([7544fac](7544fac))

### Features

* **boilerplate:** upgrade to Expo SDK 51 ([#2683](#2683) by [@frankcalise](https://github.com/frankcalise)) ([e0f3aa8](e0f3aa8)), closes [#2692](#2692)
@infinitered-circleci
Copy link

🎉 This PR is included in version 9.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants