-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
route.params is undefined, nicer if it was an empty Object {} by default #6851
Description
React Navigation v5's API is so much better! This is a tiny thing.
Current Behavior
When there are no params passed to navigator.navigate, route.params is undefined. Additionally, the type definition doesn't indicate that params can be undefined.
This means that the following code will raise an exception because params is not an Object:
const {showStart, onChange} = useRoute().paramsThis makes sense. There are no params, so params is never set.
Expected Behavior
From a developer experience perspective though, It'd be a little easier if it was an empty Object when undefined. That way, I can use destructuring without optional chaining.
How to reproduce
Navigate to a route but don't pass any params to it.
const {showStart, onChange} = useRoute().paramsYour Environment
├─ @react-navigation/core@5.1.2
│ ├─ @react-navigation/routers@^5.0.1
├─ @react-navigation/native-stack@5.0.3
├─ @react-navigation/native@5.0.3
│ └─ @react-navigation/core@^5.1.2
├─ @react-navigation/routers@5.0.1
├─ @react-navigation/stack@5.0.3
├─ react-navigation-animated-switch@0.4.1
├─ react-navigation-shared-element@0.5.0
--
In terms of how to implement this...maybe it could be an Object.defineProperty getter on the route object returned by useRoute with enumerable set to false, so that it doesn't show up if you JSON.stringify it, but its at least accessible?
