Conditional React Navigation header button

前端 未结 1 1435
轮回少年
轮回少年 2021-01-25 00:41

I am trying to make a button appear in headerRight of the header bar in a React Navigation screen, but I cant seem to get it to work.

I want the start butto

相关标签:
1条回答
  • 2021-01-25 01:31

    You can use the same mechanics describe here for title: https://reactnavigation.org/docs/en/headers.html#setting-the-header-title

    Set a navigation params and use it on your navigationOptions.

    In your case:

    state = { players: 0 };
    
    static navigationOptions = ({ navigation }) => {
      return {
        headerRight: navigation.getParam('players', 0) > 1 ? <YourHeaderButtonComponent /> : null ,
      };
    };
    
    addPlayer = () => {
        this.setState(({players}) => {
            this.props.navigation.setParams({players: players + 1})
            return {players: players + 1 }
        });
    }
    
    render {
        ...
        <Button onPress={this.addPlayer}
        ...
    }
    
    0 讨论(0)
提交回复
热议问题