fix: return undefined for buildLink if linking is not enabled

This commit is contained in:
Satyajit Sahoo
2020-05-04 06:35:22 +02:00
parent 6bec620a3f
commit 9fd2635756
2 changed files with 15 additions and 3 deletions

View File

@@ -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 }],
};

View File

@@ -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);
}
}