From 9fd2635756362c8da79656b4d9b101bebaaf7003 Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Mon, 4 May 2020 06:35:22 +0200 Subject: [PATCH] fix: return undefined for buildLink if linking is not enabled --- packages/native/src/useLinkBuilder.tsx | 10 +++++++--- packages/native/src/useLinkProps.tsx | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/native/src/useLinkBuilder.tsx b/packages/native/src/useLinkBuilder.tsx index 6094cfaf..0d5e9917 100644 --- a/packages/native/src/useLinkBuilder.tsx +++ b/packages/native/src/useLinkBuilder.tsx @@ -52,14 +52,18 @@ export default function useLinkBuilder() { (name: string, params?: object) => { const { options } = linking; - // If we couldn't find a navigation object in context, we're at root - // So we'll construct a basic state object to use + if (options?.enabled === false) { + return undefined; + } + const state = navigation ? getRootStateForNavigate(navigation, { index: 0, routes: [{ name, params }], }) - : { + : // If we couldn't find a navigation object in context, we're at root + // So we'll construct a basic state object to use + { index: 0, routes: [{ name, params }], }; diff --git a/packages/native/src/useLinkProps.tsx b/packages/native/src/useLinkProps.tsx index d90ba1bd..e907fdfa 100644 --- a/packages/native/src/useLinkProps.tsx +++ b/packages/native/src/useLinkProps.tsx @@ -49,6 +49,14 @@ export default function useLinkProps({ to, action }: Props) { throw new Error("Couldn't find a navigation object."); } } else { + if (typeof to !== 'string') { + throw new Error( + `To 'to' option is invalid (found '${String( + to + )}'. It must be a valid string for navigation.` + ); + } + linkTo(to); } }