React-native/react-navigation: how do I access a component's state from `static navigationOptions`?

前端 未结 4 1355
野趣味
野趣味 2020-12-15 07:11

How do you handle cases when you have, say, a form component, and you need to submit a part of the component\'s state using button in navigation bar?



        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 07:50

    Simple Design Pattern

    Just as a follow-up to @val's excellent answer, here's how I structured my Component so that all the params are set in the componentWillMount. I find this keeps it simpler and is an easy pattern to follow for all other screens.

    static navigationOptions = ({navigation, screenProps}) => {
      const params = navigation.state.params || {};
    
      return {
        title:       params.title,
        headerLeft:  params.headerLeft,
        headerRight: params.headerRight,
      }
    }
    
    _setNavigationParams() {
      let title       = 'Form';
      let headerLeft  = 

提交回复
热议问题