React Native - CSS :last-child without a border?

前端 未结 2 958
无人及你
无人及你 2021-02-11 20:05

In React Native, I have a static array of books that I map over and output in stacked rows like this:

return books.map((book, i) => {
  return(
    

        
相关标签:
2条回答
  • 2021-02-11 20:38

    You can try extended-stylesheet that supports :last-child:

    import EStyleSheet from 'react-native-extended-stylesheet';
    
    const styles = EStyleSheet.create({
      book: {
        borderBottomWidth: 1,
        borderBottomColor: '#000000'
      },
      'book:last-child': {
        borderBottomWidth: 0
      }
    });
    
    return books.map((book, i) => {
      const style = EStyleSheet.child(styles, 'book', i, book.length);
      return(
        <View style={style} key={i}>
          <Text style={style}>{book.title}</Text>
        </View>
      );
    });
    
    0 讨论(0)
  • 2021-02-11 20:39

    You could achieve this using some logic:

    return books.map((book, i) => {
      return(
        <View style={ (i === books.length - 1) ? styles.noBorderBook : styles.book} key={i}>
          <Text style={(i === books.length - 1) ? styles.noBorderBook : styles.book}>{book.title}</Text>
        </View>
      );
    });
    

    What this does is check if i, the iterator, is equal to the books array's length - 1.

    0 讨论(0)
提交回复
热议问题