Skip to content

route.params is undefined, nicer if it was an empty Object {} by default #6851

@Jarred-Sumner

Description

@Jarred-Sumner

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().params

image

This 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().params

Your 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions