访问静态对象内的类

时间:2017-10-10 14:04:47

标签: reactjs react-native ecmascript-6

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

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>
        )
    }
}

1 个答案:

答案 0 :(得分:2)

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