render(){
return(
//这里调用.length是成功执行的
{alert(this.props.rightTitle.length)}}>
{this.props.title}
{this.renderRightView()}
)
}
renderRightView=()=>{
if(this.props.isSwitch){
return (
{this.setState({isOn:!this.state.isOn})}} style={{marginRight:8}}/>
)
}
else{
return(
{this.rightTitleView()}
)
}
}
rightTitleView(){
//这里调用.length是会报错的
if(this.props.rightTitle.length)
{
return(
{this.props.rightTitle}
)
}
}
具体错误是:undefined is not an object (evaluating this.props.rightTitle.length)
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
rightTitleView需要绑定this,否则获取不了props。
好端端的renderRightView()为什么要写成箭头函数?
箭头函数是回调,回调就要bind(this)。
这边this已经指向TouchableOpacity 了,当然没了,可以手动绑定this,一般是在constructor()函数中,
也可以用autobind-decorator库来解决这个问题,但是这个需要用到装饰器,需要进行转换一下,配置一下babel就可以