2017-10-14 24 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與你的答案相同,所以我只是接受了他的答案。抱歉! –