2017-10-14 26 views
1

TaskQueue: Error with task : undefined is not a function (evaluating 'this._renderReplies(replyCount)')为什么我的函数是未定义的?

我在上面得到这个错误。

_renderReplies = (replyCount) => { 
    return (<Text>`View ${replyCount} replies`</Text>); 
} 

_renderItem(row) { 
      ... 
     <View>{this._renderReplies(replyCount)}</View> <- Getting an error here 
     </View> 
    ) 
    } 

为什么我收到未定义功能错误????太奇怪了。

+1

我想你忘了绑定'_renderItem'方法,正因为如此,检查。 –

+0

我不认为我需要绑定它,因为我正在使用这个语法'_renderReplies =(replyCount)=>'我还需要绑定吗? –

+0

但你没有使用'_renderItem'的箭头函数,使用相同的方式它会工作:) –

回答

3

this关键字你应该结合你的_renderItem功能的class;

要么使用arrow功能(像你_renderReplies没有),它会自动将其绑定

_renderItem = (row) => { 
      ... 
     <View>{this._renderReplies(replyCount)}</View> <- Getting an error here 
     </View> 
    ) 
    } 

或结合在constructor

constructor(props){ 
    super(props); 
    this._renderItem = this._renderItem.bind(this); 
} 
_renderItem(row) { 
      ... 
     <View>{this._renderReplies(replyCount)}</View> <- Getting an error here 
     </View> 
    ) 
    } 
0

尽量不要使用,当你调用_renderReplies()

+0

恩,它没有工作 –

2

_renderItem没有访问这个。您可以使用箭头函数或将其绑定到构造函数中。箭头功能始终可以访问此功能。

箭功能的方法:

_renderItem = (row) => { 
      ... 
     <View>{this._renderReplies(replyCount)}</View> <- Getting an error here 
     </View> 
    ) 
} 

_renderReplies = (replyCount) => { 
    return (<Text>`View ${replyCount} replies`</Text>); 
} 

绑定方法:

constructor(props) { 
    this._renderItem = this._renderItem.bind(this) 
    this._renderReplies = this._renderReplies.bind(this) 
} 

_renderItem(row) { 
      ... 
     <View>{this._renderReplies(replyCount)}</View> <- Getting an error here 
     </View> 
    ) 
} 

_renderReplies(replyCount) { 
    return (<Text>`View ${replyCount} replies`</Text>); 
} 
+0

感谢您的输入! Sag1v与你的答案相同,所以我只是接受了他的答案。抱歉! –