0

I want to use if statement in map function. Here is the code which I am using:

_collapsible() {
    return (
        <View style={styles.section}>
            <View style={CssHelper['flexRowCentered']}>
                { data['items'].map((item, index) =>
                    <FilterButton key={index} status={STATUS_DEFAULT_FILTER_BUTTON} style={styles.brandContainer}>
                        <ImageBackground style={styles.brand} source={item.source} resizeMode="contain"/>
                    </FilterButton>

                    //((index + 1) % 3 === 0) && <View style={CssHelper['verticalDivider']}></View>
                )}
            </View>
        </View>
    );
}
2
  • I think this is more to do with returning multiple child components instead of a single component from your .map call - try wrapping the return value of the map function in a <React.Fragment>. Commented Nov 21, 2019 at 6:22
  • Ok, thanks. Can you show me a little code snippet how to do it? Commented Nov 21, 2019 at 6:27

2 Answers 2

3

Your code is almost correct, The only thing is you miss a double bracket before the condition checking, Also please try to add a wrapper inside the map function to hold all the content, I have added a sample code for the same case with wrapper content inside map with flex:1

{ this.state.items.map((item, index) =>
    <View  style={{flex:1}} key={index}>
        <Text>{item.title}</Text>
        {  
        ((index + 1) % 3 === 0) && 
        <View style={{}}><Text>Third Row</Text></View> 
        } 
    </View>
)}
Sign up to request clarification or add additional context in comments.

Comments

1

use ternary condition

{statment ? : null}

Comments

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.