fix: make getInitialState async on web

This commit is contained in:
Satyajit Sahoo
2020-02-11 12:09:07 +01:00
parent 0c59ef7328
commit 6c6102b459
2 changed files with 5 additions and 5 deletions

View File

@@ -18,6 +18,7 @@ export type LinkingOptions = {
/**
* The prefixes are stripped from the URL before parsing them.
* Usually they are the `scheme` + `host` (e.g. `myapp://chat?user=jane`)
* Only applicable on Android and iOS.
*/
prefixes: string[];
/**
@@ -36,6 +37,7 @@ export type LinkingOptions = {
config?: Parameters<typeof getStateFromPathDefault>[1];
/**
* Custom function to parse the URL to a valid navigation state (advanced).
* Only applicable on Web.
*/
getStateFromPath?: typeof getStateFromPathDefault;
/**

View File

@@ -32,7 +32,6 @@ let isUsingLinking = false;
export default function useLinking(
ref: React.RefObject<NavigationContainerRef>,
{
prefixes,
config,
getStateFromPath = getStateFromPathDefault,
getPathFromState = getPathFromStateDefault,
@@ -55,19 +54,18 @@ export default function useLinking(
// We store these options in ref to avoid re-creating getInitialState and re-subscribing listeners
// This lets user avoid wrapping the items in `React.useCallback` or `React.useMemo`
// Not re-creating `getInitialState` is important coz it makes it easier for the user to use in an effect
const prefixesRef = React.useRef(prefixes);
const configRef = React.useRef(config);
const getStateFromPathRef = React.useRef(getStateFromPath);
const getPathFromStateRef = React.useRef(getPathFromState);
React.useEffect(() => {
prefixesRef.current = prefixes;
configRef.current = config;
getStateFromPathRef.current = getStateFromPath;
getPathFromStateRef.current = getPathFromState;
}, [config, getPathFromState, getStateFromPath, prefixes]);
}, [config, getPathFromState, getStateFromPath]);
const getInitialState = React.useCallback(() => {
// Make it an async function to keep consistent with the native impl
const getInitialState = React.useCallback(async () => {
const path = location.pathname + location.search;
if (path) {