簡體   English   中英

如何在反應原生的標題選項卡導航中訪問變量

[英]How to access a variable in header tab navigation in react native

我的選項卡標題中有一個開關,每次切換開關時,我都想在標題中獲取開關值的值。 我怎樣才能得到那個值?

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,
  });
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM