实例成员不可访问 [英] instance member is not accessible

查看:47
本文介绍了实例成员不可访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从堆栈导航器标头内部访问类函数?这可能吗?我想要实现的是在按下堆栈导航器标题时调用一个函数.

How can I access class functions from inside stack navigator header? Is this possible? What I'm trying to achieve is to call a function when I press the stack navigator header title.

class Dashboard extends React.Component {
        static navigationOptions = ({ navigation }) => {
            return {
                headerTitle: (
                        <View style={header.addressView}>
                            <Text
                                style={header.titleAddress}
                                onPress={() => {
                                  this._show().bind(this);
                                }}>
                            />
                        </View>
                ),
            };
        };

        _show(){
            this.setState({ visibleModal: 1 })
        }

        constructor(props) {
            super(props);

            this.state = {
                visibleModal: null,
            };
        }

        render() {
            ...........

        }
    }

    export default Dashboard;

推荐答案

class Dashboard extends React.Component {
    static navigationOptions = ({ navigation }) => {
        const showParams = navigation.getParam("show",()=>{});
        //Access the params like this.
        return {
            headerTitle: (
                    <View style={header.addressView}>
                        <Text
                            style={header.titleAddress}
                            onPress={showParams}>
                        />
                    </View>
            ),
        };
    };

    _show(){
        this.setState({ visibleModal: 1 })
    }

    //Too add this.

    componentDidMount(){
      this.props.navigation.setParams({show:()=>this._show()});
    }

    constructor(props) {
        super(props);

        this.state = {
            visibleModal: null,
        };
    }

    render() {
        ...........

    }
}

export default Dashboard;

这是访问类中的变量或状态并使其可用于静态导航选项函数的方式.

This is how you access the variables or states inside a class and make it available for the static navigationOptions function.

参考

这篇关于实例成员不可访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆