Skip to content

Commit 4b36bcf

Browse files
committed
fix: use console.error for linking conflicts instead of throwing
1 parent 171d7e1 commit 4b36bcf

File tree

3 files changed

+62
-39
lines changed

3 files changed

+62
-39
lines changed

packages/native/src/__tests__/useLinking.test.tsx

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ it('throws if multiple instances of useLinking are used', () => {
1818
return null;
1919
}
2020

21+
const spy = jest.spyOn(console, 'error').mockImplementation();
22+
2123
let element: RenderAPI | undefined;
2224

23-
expect(() => (element = render(<Sample />))).toThrowError(
25+
element = render(<Sample />);
26+
27+
expect(spy).toHaveBeenCalledTimes(1);
28+
expect(spy.mock.calls[0][0]).toMatch(
2429
'Looks like you have configured linking in multiple places.'
2530
);
2631

@@ -36,15 +41,17 @@ it('throws if multiple instances of useLinking are used', () => {
3641
return null;
3742
}
3843

39-
expect(
40-
() =>
41-
(element = render(
42-
<>
43-
<A />
44-
<B />
45-
</>
46-
))
47-
).toThrowError('Looks like you have configured linking in multiple places.');
44+
element = render(
45+
<>
46+
<A />
47+
<B />
48+
</>
49+
);
50+
51+
expect(spy).toHaveBeenCalledTimes(2);
52+
expect(spy.mock.calls[1][0]).toMatch(
53+
'Looks like you have configured linking in multiple places.'
54+
);
4855

4956
element?.unmount();
5057

@@ -57,17 +64,9 @@ it('throws if multiple instances of useLinking are used', () => {
5764

5865
render(wrapper2).unmount();
5966

60-
expect(() => (element = render(wrapper2))).not.toThrow();
61-
62-
element?.unmount();
63-
64-
function Sample3() {
65-
useLinking(ref, options);
66-
useLinking(ref, { ...options, enabled: false });
67-
return null;
68-
}
67+
render(wrapper2);
6968

70-
expect(() => (element = render(<Sample3 />))).not.toThrowError();
69+
expect(spy).toHaveBeenCalledTimes(2);
7170

7271
element?.unmount();
7372
});

packages/native/src/useLinking.native.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Options = LinkingOptions<ParamListBase> & {
1616
independent?: boolean;
1717
};
1818

19-
let isUsingLinking = false;
19+
let linkingHandlers: Symbol[] = [];
2020

2121
export default function useLinking(
2222
ref: React.RefObject<NavigationContainerRef<ParamListBase>>,
@@ -56,31 +56,43 @@ export default function useLinking(
5656
}: Options
5757
) {
5858
React.useEffect(() => {
59+
if (process.env.NODE_ENV === 'production') {
60+
return undefined;
61+
}
62+
5963
if (independent) {
6064
return undefined;
6165
}
6266

63-
if (enabled !== false && isUsingLinking) {
64-
throw new Error(
67+
if (enabled !== false && linkingHandlers.length) {
68+
console.error(
6569
[
6670
'Looks like you have configured linking in multiple places. This is likely an error since deep links should only be handled in one place to avoid conflicts. Make sure that:',
67-
"- You are not using both 'linking' prop and 'useLinking'",
68-
"- You don't have 'useLinking' in multiple components",
71+
"- You don't have multiple NavigationContainers in the app each with 'linking' enabled",
72+
'- Only a single instance of the root component is rendered',
6973
Platform.OS === 'android'
7074
? "- You have set 'android:launchMode=singleTask' in the '<activity />' section of the 'AndroidManifest.xml' file to avoid launching multiple instances"
7175
: '',
7276
]
7377
.join('\n')
7478
.trim()
7579
);
76-
} else {
77-
isUsingLinking = enabled !== false;
80+
}
81+
82+
const handler = Symbol();
83+
84+
if (enabled !== false) {
85+
linkingHandlers.push(handler);
7886
}
7987

8088
return () => {
81-
isUsingLinking = false;
89+
const index = linkingHandlers.indexOf(handler);
90+
91+
if (index > -1) {
92+
linkingHandlers.splice(index, 1);
93+
}
8294
};
83-
});
95+
}, [enabled, independent]);
8496

8597
// We store these options in ref to avoid re-creating getInitialState and re-subscribing listeners
8698
// This lets user avoid wrapping the items in `React.useCallback` or `React.useMemo`

packages/native/src/useLinking.tsx

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ const series = (cb: () => Promise<void>) => {
287287
return callback;
288288
};
289289

290-
let isUsingLinking = false;
290+
let linkingHandlers: Symbol[] = [];
291291

292292
type Options = LinkingOptions<ParamListBase> & {
293293
independent?: boolean;
@@ -305,28 +305,40 @@ export default function useLinking(
305305
}: Options
306306
) {
307307
React.useEffect(() => {
308+
if (process.env.NODE_ENV === 'production') {
309+
return undefined;
310+
}
311+
308312
if (independent) {
309313
return undefined;
310314
}
311315

312-
if (enabled !== false && isUsingLinking) {
313-
throw new Error(
316+
if (enabled !== false && linkingHandlers.length) {
317+
console.error(
314318
[
315-
'Looks like you have configured linking in multiple places. This is likely an error since URL integration should only be handled in one place to avoid conflicts. Make sure that:',
316-
"- You are not using both 'linking' prop and 'useLinking'",
317-
"- You don't have 'useLinking' in multiple components",
319+
'Looks like you have configured linking in multiple places. This is likely an error since deep links should only be handled in one place to avoid conflicts. Make sure that:',
320+
"- You don't have multiple NavigationContainers in the app each with 'linking' enabled",
321+
'- Only a single instance of the root component is rendered',
318322
]
319323
.join('\n')
320324
.trim()
321325
);
322-
} else {
323-
isUsingLinking = enabled !== false;
326+
}
327+
328+
const handler = Symbol();
329+
330+
if (enabled !== false) {
331+
linkingHandlers.push(handler);
324332
}
325333

326334
return () => {
327-
isUsingLinking = false;
335+
const index = linkingHandlers.indexOf(handler);
336+
337+
if (index > -1) {
338+
linkingHandlers.splice(index, 1);
339+
}
328340
};
329-
});
341+
}, [enabled, independent]);
330342

331343
const [history] = React.useState(createMemoryHistory);
332344

0 commit comments

Comments
 (0)