1

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.

4
  • 2
    You question has nothing to do with React :) Commented Oct 4, 2017 at 7:43
  • 1
    where is the loop (for, while, forEach, map, etc)? 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. Commented Oct 4, 2017 at 7:47
  • sorry the loop is a fetch statement from the server using API. Commented Oct 4, 2017 at 7:56

2 Answers 2

10

Your loop() is actually a function which you can return from in order to escape it.

loop(){
  if(){
    return;
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

return is not a solution if there are more other loop after this.
@Ved without more detail from the OP, this is the only possible solution.
I agree with you.
2

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.

3 Comments

I think break in JavaScript is only used inside a switch case?
break condition can be used inside for loop.
break can be used inside any loop including for and while

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.