I've been wondering how to stop your loop, like break
in some programming languages but in React js seems don't working.
something like this:
loop(){
if(){
//meet the if statement end the loop.
break; //not working.
}
}
no actually the loop is a fetch from the server using aPI.
I've been wondering how to stop your loop, like break
in some programming languages but in React js seems don't working.
something like this:
loop(){
if(){
//meet the if statement end the loop.
break; //not working.
}
}
no actually the loop is a fetch from the server using aPI.
Share Improve this question edited Oct 4, 2017 at 7:54 Dave Niaga Lape asked Oct 4, 2017 at 7:42 Dave Niaga LapeDave Niaga Lape 371 gold badge1 silver badge6 bronze badges 4- 2 You question has nothing to do with React :) – David Guan Commented Oct 4, 2017 at 7:43
-
1
where is the loop (
for
,while
,forEach
,map
, etc)? – Davin Tryon Commented Oct 4, 2017 at 7:43 - More detail about how this loop function is called is required for you to get a good answer. – Canastro Commented Oct 4, 2017 at 7:47
- sorry the loop is a fetch statement from the server using API. – Dave Niaga Lape Commented Oct 4, 2017 at 7:56
2 Answers
Reset to default 10Your loop()
is actually a function which you can return
from in order to escape it.
loop(){
if(){
return;
}
}
You can create a exit statement for if
. And break
do not work with if
condition.
loop(){
let breakCondition = false;
if(actualCondition && !breakCondition ){
//here actualCondition is the expression you are using in if.
breakCondition = true
}
}
And If there is no other code below the
if
condition than check @Soviut answer.