instance member is not accessible

一笑奈何 提交于 2019-12-11 16:34:12

问题


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;

回答1:


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.

Reference



来源:https://stackoverflow.com/questions/51444014/instance-member-is-not-accessible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!