Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Consistent availability of `navigation.state.params` #1048

Closed
atshakil opened this issue Apr 14, 2017 · 6 comments

Comments

@atshakil
Copy link

@atshakil atshakil commented Apr 14, 2017

Currently, when there is no param available for a screen, navigation prop doesn't include a params property.

For an example, when some param is passed, navigationOptions of a screen gets,

"props": {
  "navigation": {
    "state": {
      "routeName": "S1",
      "key": "Init",
      "params": {
        "headless": true,
        "title": "Screen 1"
      }
    }
  },
  "screenProps": {},
  "navigationOptions": {}
}

but, when no param is passed, the screen gets

"props": {
  "navigation": {
    "state": {
      "routeName": "S6",
      "key": "Init"
    }
  },
  "screenProps": {},
  "navigationOptions": {}
}

This leads to difficulties in checking screen params. Consider the following scenario. In order to set a property value based on the availability of a specific param, we'll have to write,

navigationOptions({navigation}) {
  return {
    headerVisible: !(navigation.state.params && navigation.state.params.headless)
  };
}

But, if params had a consistent availability (e.g. params: {}) like screenProps or navigationOptions, then we could just write,

navigationOptions({navigation}) {
  return {
    headerVisible: !navigation.state.params.headless
  };
}
@jamesisaac

This comment has been minimized.

Copy link
Contributor

@jamesisaac jamesisaac commented Apr 24, 2017

Agree it would be nice, but in the mean time you can write your example as

navigationOptions({ navigation }) {
  const { params = {} } = navigation.state
  return {
    headerVisible: !params.headless,
  };
}

or

navigationOptions = ({ navigation: { state: { params = {} } } }) => ({
  headerVisible: !params.headless,
})
@Jacse

This comment has been minimized.

Copy link
Contributor

@Jacse Jacse commented Apr 30, 2017

I think this would be a very meaningful change, just like React Components without props have a default empty object. While the workaround posted by @jamesisaac will work, it is not exactly a clean or intuitive solution.

@kelset

This comment has been minimized.

Copy link
Contributor

@kelset kelset commented Oct 21, 2017

Pinging OP @atshakil since this issue is quite old and related to an old version of the lib. If no answer I'll close in 7 days.

@dantman

This comment has been minimized.

Copy link
Contributor

@dantman dantman commented Oct 21, 2017

@kelset There haven't been any changes to react-navigation that fix this issue.

@kelset

This comment has been minimized.

Copy link
Contributor

@kelset kelset commented Oct 21, 2017

@dantman understood. I'll keep this open then. I actually think there are some other issues (and maybe even a PR that tackle this issue) but anyway I'll keep this open for the time being.

@brentvatne

This comment has been minimized.

Copy link
Member

@brentvatne brentvatne commented Jan 23, 2018

we should make a getter for this: this.props.navigation.getParam(key, defaultValue)

moved this to https://react-navigation.canny.io/feature-requests/p/easier-to-use-params-via-helper-or-default-object

@brentvatne brentvatne closed this Jan 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.