在React Native中如何在标题标签导航中访问变量

时间:2019-12-04 09:19:21

标签: react-native react-native-navigation wix-react-native-navigation

我的标签标题中有一个开关, 每次切换开关时,我都希望在标头中获取switchValue的值。

   static navigationOptions = ({ navigation }) => {
    const { params = {} } = navigation.state;

return {
    title: "Home",
    headerTitleStyle: { flex: 1, textAlign: 'center',color:'white',fontSize: 20},
    headerTintColor:'white',
    headerStyle: {
        backgroundColor: '#4169E1',
    },
    headerRight:    <Switch 
    onValueChange = {() => params.handleSave()}
    value = {this.navigation.state.switchValue}/>
};
};


  class HomeScreen  extends React.Component{
  state = {switchValue:false}
 componentDidMount() {
this.props.navigation.setParams({ handleSave: this.toggleSwitch });
  }
 toggleSwitch = (value) => {
    //onValueChange of the switch this function will be called
  this.setState({switchValue: value})
  //state changes according to switch
  //which will result in re-render the text
  }
  }`

1 个答案:

答案 0 :(得分:0)

每次我更新navigationOptions中的params值时,我都只是调用this.props.navigation.setParams

 toggleSwitch = (value) => {
this.setState({switchValue: value})
 this.props.navigation.setParams({
    switchValue: holder,
  });
      }
   }`