【问题标题】:Access to class inside the static object访问静态对象内部的类
【发布时间】:2017-10-10 14:04:46
【问题描述】:

请帮助我从静态导航选项内部访问 Page1 类。 仅供参考,请不要建议使用 .this,因为它不起作用,并且必须在 navigationOptions 中使用静态。

class Page1 extends Component {
    constructor(){
        super();
        this.state = {
            isCollapsed:false
        }
    }
    static navigationOptions = {
        drawerLabel: () => (
            <View style={{
                flexDirection:'row',
                alignItems:'center',
                justifyContent:'space-around',
                width:'100%'
            }}>
                <Text>Page 1</Text>
                <TouchableOpacity
                    activeOpacity={0.9}
                    onPress={() => this.setState({isCollapsed:!this.state.isCollapsed})}
                >
                    <Icon
                        name='plus'
                        type='entypo'
                    />
                </TouchableOpacity>
                <Collapsible collapsed={this.state.isCollapsed}>
                    <Text>Items...</Text>
                </Collapsible>
            </View>
        ),
    };

    render(){
        return(
            <View>
                <Text>Page 1</Text>
            </View>
        )
    }
}

【问题讨论】:

    标签: reactjs react-native ecmascript-6


    【解决方案1】:

    静态成员不能使用this 访问类实例的值和属性,因为静态方法是类的属性而不是特定实例。见this。
    您可以通过全局声明 var self = null 然后在构造函数中分配 self = this 来进行臭名昭著的“自我”黑客攻击。

    【讨论】:

    • “你可以通过全局声明 var self = null 然后在构造函数中分配 self = this 来进行臭名昭著的 'self' hack。” 这假设总是只有一个类的单个实例?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    相关资源
    最近更新 更多