如何在 header 选项卡导航中访问本机中的变量

How to access a variable in header tab navigation in react native

我的选项卡中有一个开关 header 并且 每次切换开关时,我都想在 header 中获取开关值的值。我怎样才能得到那个值?

const 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
  };
}

我每次更新导航选项中的参数值时都会调用 this.props.navigation.setParams

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