2017-02-03 52 views
0

我想調用一個按鈕上的函數,在React Native中單擊。從反應本機錯誤調用一個函數

onPress = {callButton} 

和函數調用

const busButtonPress() { 
    fetch('http://server/api') 
    .then((response) => response.json()) 
    .then((responseJSON) => { 
    console.log(responseJSON); 
}) 
.catch((error) => { 
    console.warn(error); 
}); 
}; 

這是我得到的錯誤!

Error Pic

這是奇怪的例外,我得到。請幫忙!

+0

did you mean const busButtonPress = function()? – romuleald

+0

你的功能被稱爲busButtonPress ...所以它應該是'onPress = {busButtonPress}' –

+0

馬特,對不起這是一個錯誤,我叫busButtonPress只,不幸的是在這裏寫錯了 –

回答

0

你還沒有真正發佈足夠的代碼真正看到可能出現的錯誤,但按下觸摸屏是非常簡單的。下面是一個簡單的例子:

export default class Weather extends Component { 
    constructor(props) { 
     super(props); 
    } 

    _buttonPress =() => { 
     // your fetch code here 
    }; 

    render() { 
     return(
      <View style={{flex: 1}}> 
       <TouchableOpacity onPress={this._buttonPress}> 
        <Text>Press Up On Me</Text> 
       </TouchableOpacity> 
      </View> 
     ); 
    } 
} 

務必從陣營本地導入您的必要組成部分,而這一切,你必須做的。沒有更多的代碼,正如我所說的,很難調試你的具體錯誤。有關更多信息,請參閱官方React Native文檔:TouchableOpacityTouchableHighlightTouchableWithoutFeedback

+0

非常感謝馬修科韋,但爲什麼這個按鈕功能不起作用?我們如何在按鍵按下時撥打同樣的電話? –