简体   繁体   English

如何在按钮上动态加载API数据在React Native中单击

[英]How to dynamically Load API Data on a button Click in React Native

I am a starter in React Native. 我是React Native的入门者。 I have an API that dynamically displays the number of levels on a page. 我有一个API,可以在页面上动态显示级别数。 Every time the user clicks on the button, I want to fetch an API and navigate to another page. 每次用户单击按钮时,我要获取一个API并导航到另一个页面。 I have defined three on click events for this, but I want to clean up my code and write with the help of one OnClick event. 我为此定义了三个on click事件,但是我想清理代码并借助一个OnClick事件进行编写。

My levels are as follows 我的等级如下

//Level1
async Level1() {
    const datavalue = await getResultValue('url1');
    this.props.navigation.navigate('Final','getSomething');
}
//Level2
async Level12() {
    const datavalue = await getResultValue('url2');
    this.props.navigation.navigate('Final','getSomething');
}
//Level3
async Level3() {
    const datavalue = await getResultValue('url3');
    this.props.navigation.navigate('Final','getSomething');
} 

Each an every level, there is a corresponding API. 每个每个级别,都有一个对应的API。 Is there a way I can achieve it with the help of One OnClick Event? 有没有一种方法可以借助One OnClick Event来实现?

Any help would be appreciated. 任何帮助,将不胜感激。 Thank you in advance. 先感谢您。

async navigateToFinalScreen(params){
  data = await fetch(params.url);
  this.props.navigation.navigate(params.screenToNavigate, 'getSomething'); 
}   

/* calling This method */ / *调用此方法* /

 this.navigateToNextScreen(
   { url:"url",
     screenToNavigate:"Final" }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM