实例成员不可访问

时间:2018-07-20 13:47:46

标签: react-native react-navigation

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

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;

1 个答案:

答案 0 :(得分:2)

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;

这是您访问类内的变量或状态并使之可用于静态NavigationOptions函数的方式。

Reference