在onPress上隐藏和显示reactnavigation标头

问题描述 投票:0回答:1

我正在使用React Navigation并希望隐藏/显示标题onScroll或onPress。这个伪代码是正确的方法吗?另外,请问我需要通过哪些道具以及如何从_handleHide_handleShow函数传递它们?

import React, { Component } from 'react'
import { View, Text, StyleSheet, Button} from 'react-native'

class MyApp extends Component {
    static navigationOptions = {
        title: 'MyTitle'         // this is the header I want to hide/show
    }

    constructor () {
        super(props);
        this.state = {
          showHeader: false
        }
        this._handleHide = this._handleHide.bind(this);
        this._handleShow = this._handleShow.bind(this);
    }

    _handleHide(){
        // how do i code this to hide the header?
    }
    _handleShow(){
        // how do i code this to show the header?
    }
    render(){
        return(
            <View style={styles.container}>
            <Button onPress={this._handleHide} title="Hide Header" />
            <Button onPress={this._handleShow} title="Show Header" />
            </View>
        )
    }
}

const styles = StyleSheet.create({
    container:{
        flex: 1, justifyContent: 'center', alignItems: 'center'
    }});

export default MyApp;

非常感谢。

更新1

_handleHide(){
    this.setState({showHeader: false});
}

_handleShow(){
    this.setState({showHeader: true});
}
reactjs react-native react-navigation
1个回答
1
投票

我提到的那篇文章没有状态变化。现在不在计算机附近,但我会在构造函数中添加一个名为showHeader: true的状态,并在_handleHide_handleShow中,更改showHeader的状态。

然后从后Dynamically hide/show header in react-native

this.props.navigation.setParams({
  header: this.state.showHeader ? whatever-you-want : null
})
© www.soinside.com 2019 - 2024. All rights reserved.