LoginSignup
5
3

More than 5 years have passed since last update.

React Native navigationOptionsの内からthis.setStateを行う

Last updated at Posted at 2017-08-22

React Native & React Navigationを使用してnavigationOptionsの内でheaderRight,headerLeftのButtonが押された時に、this.setStateをするための方法

ヘッダーの右ボタンが押された時にthis.state.Languageを "Japanese" → "English"に変更する

   constructor(props){
        super(props)
        this.state = {
            Language : "Japanese"
        }
        this.func2 = this.func2.bind(this)
        this.props.navigation.setParams({
            func1: this.func2
        })
    }
static navigationOptions = ({navigation}) => ({
        headerRight: <Button
            onPress={()=>
                navigation.state.params.func1()
            }
            title="Change">
        </Button>
    })
func2(){
        this.setState({
            Language:"English"
        })
    }
5
3
0

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
3