1

我希望能夠攻TouchableOpacity按鈕後,導航到一個新的屏幕,但我接受它說touchableopacity onpress功能(是不是一個函數)反應原住民

_this3.handleThisTap是不是一個錯誤功能。 (在 '_this3.handleThisTap()', '_ this3.handleThisTap' 是未定義)

import React, { Component } from 'react'; 

import { 
    Text, 
    StyleSheet, 
    View, 
    TextInput, 
    KeyboardAvoidingView, 
    FlatList, 
    TouchableOpacity, 
    TouchableHighlight, 
} from 'react-native'; 

import { 
    SearchBar, 
    Wrapper, 
    ItemWrapper, 
} from './searchView.styles'; 

export default class SearchView extends Component { 

    constructor(props) { 
    super(props); 
    this.state = { 
     feedUrl: 'https://api.urbandictionary.com/v0/autocomplete?term=', 
     isLoading: true, 
    } 
    } 

    handleTextChange(text) { 
    let url = this.state.feedUrl + text; 
    return fetch(url) 
     .then((response) => response.json()) 
     .then((res) => { 
     this.setState({ 
      data: res, 
      isLoading: false, 
     }) 
     }) 
     .catch((error) => { 
     console.log(error); 
     }) 
    } 

    handleThisTap(item) { 
    this.props.navigation.navigate('FeedView', item); 
    } 

    _renderItem({ item }) { 
    return (
     <ItemWrapper 
     underlayColor='white' 
     onPress={() => this.handleThisTap(item)}> 
     <Text>{item}</Text> 
     </ItemWrapper> 
    ) 
    } 

    render() { 

    return (
     <Wrapper behavior="padding"> 
     <SearchBar 
      style={{ 
      shadowOffset: { 
       width: 0, 
       height: 5, 
      }, 
      }} 
      autoFocus={true} 
      clearTextOnFocus={true} 
      placeholder="Search for text here" 
      returnKeyType='search' 
      clearButtonMode='always' 
      keyboardShouldPersistTaps={true} 
      onChangeText={(text) => 
      this.handleTextChange(text) 
      } /> 
     <FlatList 
      data={this.state.data} 
      renderItem={this._renderItem} 
     /> 
     </Wrapper> 
    ) 
    } 
} 

我已經使用bind.(this)

任何幫助理解試過。

+0

什麼是你試過的'bind。(this)'?這沒有任何意義。 @Meysam Izadmehr提到它是正確的。如果你仍然沒有得到正確的答案,那麼你的結果是有一個語法錯誤。 –

+0

這是我的不好。我應該詳細解釋我所嘗試的。 @Meysam Izadmehr的答案工作。 – iprateekk

回答

4

錯誤來自您沒有將_renderItem綁定到this。將它綁定在constructor

constructor(props) { 
    super(props); 
    this.state = { 
    feedUrl: 'https://api.urbandictionary.com/v0/autocomplete?term=', 
    isLoading: true, 
    } 
    this._renderItem = this._renderItem.bind(this); //add this line 
} 
+0

仍然是一樣的錯誤:( – iprateekk

+0

我已經更新了答案,也進行了測試,它現在正在工作中 –

+0

這工作,非常感謝,我對此很新,所以請記住,我需要綁定函數構造函數 – iprateekk

相關問題