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

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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



I am a starter in React Native. I have an API that dynamically displays the number of levels on a page. Every time the user clicks on the button, I want to fetch an API and navigate to another page. 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.



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. Is there a way I can achieve it with the help of One OnClick Event?



Any help would be appreciated. Thank you in advance.





I don't know React Native, but can't you use something like Promise.all to get all the results in one time, then set those to your state and somehow with onClick go to each one using a level counter state or something else?
– devserkan
yesterday


onClick





how do you get the level[n]? ternary operation should do the best
– flix
yesterday




level[n]


ternary operation




1 Answer
1


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



/* calling This method */


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





Thanks :) That worked !
– pooja
19 hours ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

V8x,6tJ9y0Ze,Y6iuNZasUG5,9VJ JPerUe9oT,bjP22aV,btN1t4,of37fc6yl0IU
JKm52G egc,NKvi3zbWE0JLfBSSXlRKM,rq PLigfkq w

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

415 Unsupported Media Type while sending json file over REST Template