Javascript 按时出现本机错误
单击文本时,我得到一个错误,说“undefined不是对象(计算'\u this2.categorycicked.bind')” 我认为错误是“onPress={()=>this.categorycicked.bind(this)}”,当单击按钮时,必须有不同的方法调用categorycicked函数。我的代码有什么问题Javascript 按时出现本机错误,javascript,reactjs,react-native,Javascript,Reactjs,React Native,单击文本时,我得到一个错误,说“undefined不是对象(计算'\u this2.categorycicked.bind')” 我认为错误是“onPress={()=>this.categorycicked.bind(this)}”,当单击按钮时,必须有不同的方法调用categorycicked函数。我的代码有什么问题 class CategoriesView extends React.Component { constructor(props){ super(props)
class CategoriesView extends React.Component {
constructor(props){
super(props)
}
categoryClicked(){
this.props.categoryPressed(this.props.Category);
}
renderSubCategory(){
return(
this.props.Category.sub_category.map(function(subCategory, i){
return(
<View style={styles.abcd}>
<TouchableHighlight onPress={()=>this.categoryClicked.bind(this)}>
<Text>{subCategory.title}</Text>
</TouchableHighlight>
</View>
)
})
)
}
render(){
return(
<View style={{flex:1}}>
<View style={styles.avf}>
<Text>{this.props.Category.heading}</Text>
</View>
<View style={styles.ddd}>
{this.renderSubCategory()}
</View>
</View>
)
}
}
class CategoriesView扩展了React.Component{
建造师(道具){
超级(道具)
}
类别分类(){
this.props.categoryPressed(this.props.Category);
}
renderSubCategory(){
返回(
this.props.Category.sub_Category.map(函数(子类别,i){
返回(
this.categoryClicked.bind(this)}>
{子类别.标题}
)
})
)
}
render(){
返回(
{this.props.Category.heading}
{this.renderSubCategory()}
)
}
}
这在onPress={()=>this.categoryClicked.bind(this)}>中指向sub_category.map函数。它应该指向类。可以这样做
return (
this.props.Category.sub_category.map((subCategory, i) => { // <--- this way context is preserved
// other lines
onPress={()=>this.categoryClicked.bind(this, subCategory)}>
// other lines
})
);
我相信您想要做的是onPress={this.categoryClicked.bind(this)}
而不是一个箭头函数.bind(this)
返回一个上下文正确绑定到this
的函数,因此不会调用它
另外,我建议将绑定放在构造函数中,因为您不希望每次组件重新呈现时都发生绑定。
e、 g
然后只需使用onPress={this.categoryClicked}
如果要传递子类别
,可以执行以下操作
constructor(props) {
super(props);
this.subcategoryClicked = props.Category.sub_categories.map(sub_category => this.categoryClicked.bind(this, sub_category));
}
然后在渲染
中这样使用:
this.props.Category.sub_category.map(function(subCategory, i) {
<View style={styles.abcd}>
<TouchableHighlight onPress={this.subcategoryClicked[i]}>
<Text>{subCategory.title}</Text>
</TouchableHighlight>
</View>
this.props.Category.sub_Category.map(函数(子类别,i){
{子类别.标题}
另外,我不确定这是否是一个好的模式。如果你不习惯这样做,请坚持使用
这个。categoryClicked(bind,subcategory)
。这是我不确定优化是否值得的事情之一。如果我没有在map之后保留一个返回((subcategory,I)=>{……。它不会返回任何视图。如果我在那里保留一个返回,单击文本不会调用categoryClicked函数,返回应该在那里。我只是显示一个示例OK。我认为我们还需要删除()=>从onPress是的,你也可以这样做。如果你不需要在onPress上做任何其他事情,像这样的onPress={this.categorycicked.bind(this,subCategory)}
尝试调用你的函数,比如:onPress={this.categorycicked.bind(this)}
而不是onPress={()=>this.categorycicked.bind(this)}
谢谢。它现在可以工作了。您能解释一下我如何将绑定放入构造函数中吗?这会有什么帮助呢?我是一个新的本地反应者,不明白您所说的“您不希望每次组件重新呈现时都发生绑定”是什么意思因此,每次组件重新呈现时,它都会调用呈现函数。因此,每次组件重新呈现时都会调用this.categorycked.bind(this)
。如果将其放入构造函数中,则在创建组件时只有一次。为此,请执行构造函数(props){super(props);this.categorycicked=this.categorycicked.bind(this);}
。然后在渲染中只需执行onPress={this.categorycicked}
另外,我想补充一点,如果你想传递给categoryPressed
的是子类别而不是主类别,那应该会对你有所帮助。我还需要传递一个字符串给categoryclicked。我怎么能这样做呢?现在我在Press={this.categoryclicked.bind(this,subCategory)}这稍微有点难实现,尤其是如果你的类别可以更改。我建议不要尝试优化,除非必要。但如果你真的想这样做,我已经做了一些编辑的答案。
constructor(props) {
super(props);
this.subcategoryClicked = props.Category.sub_categories.map(sub_category => this.categoryClicked.bind(this, sub_category));
}
this.props.Category.sub_category.map(function(subCategory, i) {
<View style={styles.abcd}>
<TouchableHighlight onPress={this.subcategoryClicked[i]}>
<Text>{subCategory.title}</Text>
</TouchableHighlight>
</View>