我正在为BackAndroid添加一个新的监听器:
componentWillMount () {
BackAndroid.addEventListener('hardwareBackPress', backAndroidHandler.bind(this));
}
我正在删除侦听器:
componentWillUnmount () {
BackAndroid.removeEventListener('hardwareBackPress', backAndroidHandler);
}
function backAndroidHandler () {
...
}
但是当我卸载组件并再次安装它时,有两个相同的功能,同一个事件会触发两次。任何线索?
答案 0 :(得分:3)
修正了它
componentWillMount () {
this.androidBackHandler = backAndroidHandler.bind(this);
BackAndroid.addEventListener('hardwareBackPress', this.backAndroidHandler);
}
componentWillUnmount () {
BackAndroid.removeEventListener('hardwareBackPress', this.androidBackHandler);
}
function backAndroidHandler () {
...
}
我尝试直接在组件上声明函数,但this
没有绑定,它是undefined
答案 1 :(得分:1)
class DocStringInheritor(type):
"""
A variation on
http://groups.google.com/group/comp.lang.python/msg/26f7b4fcb4d66c95
by Paul McGuire
"""
def __new__(meta, name, bases, clsdict):
if not('__doc__' in clsdict and clsdict['__doc__']):
for mro_cls in (mro_cls for base in bases for mro_cls in base.mro()):
doc=mro_cls.__doc__
if doc:
clsdict['__doc__']=doc
break
for attr, attribute in clsdict.items():
if not attribute.__doc__:
for mro_cls in (mro_cls for base in bases for mro_cls in base.mro()
if hasattr(mro_cls, attr)):
doc=getattr(getattr(mro_cls,attr),'__doc__')
if doc:
if isinstance(attribute, property):
clsdict[attr] = property(attribute.fget, attribute.fset, attribute.fdel, doc)
else:
attribute.__doc__=doc
break
return type.__new__(meta, name, bases, clsdict)
class X(object):
__metaclass__ = DocStringInheritor
"""
some doc
"""
x = 20
def please_implement(self):
"""
I have a very thorough documentation
:return:
"""
raise NotImplementedError
@property
def speed(self):
"""
Current speed in knots/hour.
:return:
"""
return 0
@speed.setter
def speed(self, value):
"""
:param value:
:return:
"""
pass
class SpecialX(X):
def please_implement(self):
return True
@property
def speed(self):
return 10
@speed.setter
def speed(self, value):
self.sp = value
class VerySpecial(X):
p = 0
"""doc"""
def please_implement(self):
"""
:return bool: Always false.
"""
return False
def not_inherited(self):
"""
Look at all these words!
:return:
"""
help(X.speed)
help(SpecialX.speed)
help(SpecialX.please_implement)
help(VerySpecial.please_implement)
help(VerySpecial.not_inherited)
应该是这样的。