3

I'm attempting to break out of a loop, once I have found what I'm looking for, in this case a quote.

I know from using things like Java that it is in fact possible, however it is proving more difficult than I thought in RN. When I use a JS break, it simply says its a syntax error, is there any break statements in RN?

enter image description here

quotes.quotesArray.map((quote, i) => {
    if(!quote.isRead){
        return <Text key={i} style={styles.container, styles.quote}>{quotes.quotesArray[i].quotation}</Text>;                    
        break;
    } else {
       return 
    }
})
1
  • break is not avaiable in map loop, for that cases use for Commented Jan 25, 2017 at 12:48

3 Answers 3

2

There is no way to prematurely break the Array.prototype.map. It looks like you want to render the first element matching the !quote.isRead predicate. If that is the case you can use Array.prototype.find to find the first unread quote and then render it if it exists.

var unreadQuote = quotes.quotesArray.find(quote => !quote.isRead)
render() {
  return (
    unreadQuote && 
    <Text style={styles.container, styles.quote}>{unreadQuote.quotation}</Text>;
  )
}
Sign up to request clarification or add additional context in comments.

2 Comments

Mercuy It says that the break is an "Unsyntactic Break" - Does it mean there is a break statement that will work in React Native? Just curious
The break statement in React Native is identical to the break statement as it is used in the browsers. It follows the same rules. Which means they are used in while and for loops and switch statements for example.
1

I would do this with filter and map and move the render of the component to a function:

renderQuote(quote, i) {
    return (
        <Text key={i} style={styles.container, styles.quote}>
            {quote.quotation}
        </Text>
    );
}

// On JSX

{ quotes.quotesArray.filter((quote) => !quote.isRead).map((quote, i) => this.renderQuote(quote, i) }

Comments

0

The problem is that you're return starts the next loop so it never hits the break, here is another way you could set this up. Edited to for loop from forEach for break to be valid.

const quote = [];
for ( let i = 0; i < quotes.quotesArray.length; i++) {
  if (!quote.isRead[i]) {
    quote.push(<Text key={i} style={styles.container, styles.quote}>{quotes.quotesArray[i]}</Text>);
    break;
  }
}

You could also use lodash's .find() method to filter the array and return the first element that returns true in your callback and use that data for your text element.

2 Comments

break; cannot be used in that place.
@zerkms yeah totally forgot you cant break outta forEach, it would have to be set up as a regular 'for loop' for the break to work

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.