最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to have different style for items at certain locations in a list in React Native? - Stack Overflow

programmeradmin5浏览0评论

I'm trying to have different styles for the first and last items in a list, like the picture below:

Where the first card has rounded corner on the upper right and the last card has rounded corner on the lower right. I'm still figuring out how to approach this.

Is there any way to pass the location of an item in the list to the item itself so it can be applied to a different style? Or there are other better approaches?

Also, I would like to have the card to have both rounded upper and lower right corners if there's only one card present, like below:

I'm trying to have different styles for the first and last items in a list, like the picture below:

Where the first card has rounded corner on the upper right and the last card has rounded corner on the lower right. I'm still figuring out how to approach this.

Is there any way to pass the location of an item in the list to the item itself so it can be applied to a different style? Or there are other better approaches?

Also, I would like to have the card to have both rounded upper and lower right corners if there's only one card present, like below:

Share Improve this question asked Oct 3, 2017 at 18:44 bleepmehbleepmeh 1,0375 gold badges18 silver badges37 bronze badges 1
  • Can you pass the index of each ListView item? If so you can use something like this backgroundColor: (index % 2 == 0) ? '#ecf0f1' : '#fff' – Gabriel Mesquita Commented Oct 3, 2017 at 18:58
Add a comment  | 

3 Answers 3

Reset to default 20

When rendereing items with ListView/FlatList/SectionList rendering method has index parameter. You can use that index to figure out if the item is first or last and give conditional styling for that item.

Example

renderItem = ({item, index}) => {
  if (index === 0) return <ListItem style={styles.firstItem} data={item} />
  else if (index === (this.state.data.length -1)) return <ListItem style={styles.lastItem} data={item} />
  else return <ListItem style={styles.item} data={item} />
}

render() {
  return <FlatList data={this.state.data} renderItem={this.renderItem} />
}

With styled-components, you can do this:

import styled, { css } from "styled-components";

const Item = styled.div`
  background-color: red;

  ${props => props.isFirst && css`
    background-color: blue;
  `}

  ${props => props.isLast && css`
    background-color: yellow;
  `}
`;

function() {

  const items = [];

  return (
    <>
      ...
      {items.map((item, index) => {
        <Item
          key={item.id}
          isFirst={index === 0}
          isLast={index === items.length - 1 }
        />
      })}
      ...
    </>
  );
}

Much more readable imho.

Adding to @bennygenel 's answer we can use terniary operator as well

renderItem = ({item, index}) => {
   return <ListItem style={index === 0 ? styles.firstItem : index === (this.state.data.length -1) ? styles.lastItem : styles.item } data={item} />
 }

render() {
  return <FlatList data={this.state.data} renderItem={this.renderItem} />
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论