5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

React NavigationでNavigationOptionsに関数を埋め込む

Posted at

React NavigationのnavigationOptionsにクラスメソッドを渡すには少し工夫必要だった。

やりたいこと

やりたいことは以下のようなこと。ただし、navigationOptionsはstaticメソッドなのにthis.handleSaveを渡そうとしても動かない。

  static navigationOptions = ({
    headerLeft: <SaveButton onPress={this.handleSave}/>,
  })

navigationsetParamsして渡す

setParamscomponentDidMountのときに定義し、実行時に渡せればよい。
https://reactnavigation.org/docs/navigators/navigation-prop#setParams-Make-changes-to-route-params

  static navigationOptions = ({
    headerRight: <SaveButton onPress={() => navigation.state.params.handleSave()}/>,
  })

  componentDidMount() {
    this.props.navigation.setParams({
      handleSave: this.handleSave(),
    })
  }

  handleSave = () => {
    // 保存処理
  }
5
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?