question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditionally adding rightIcon to ListItem

See original GitHub issue

I’d like to add a rightIcon to a ListItem if the subtitle has a certain value (‘Notes’). I tried something like:

  renderItem = ({ item }) => {
    return (
      <ListItem
        title={item.title}
        subtitle={item.subtitle}
        topDivider
        bottomDivider
        {item.subtitle === 'Notes' &&
            rightIcon={{ name: 'edit', type: 'material' }}
        }
      />
    );
  };

but I get Parsing error: Unexpected token, expected "..."

Is my syntax wrong, or is this not possible?

I got it to work with a ternary that returns the whole ListItem with or without the rightIcon, but it’s more code duplication than I’d like, I’d like just the ListItem to be conditional.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
antgelcommented, Sep 9, 2019

@iRoachie Thank you so much. I believe I need to learn more about the ... operator. 😃

1reaction
antgelcommented, Sep 25, 2019

(By the way, I decided that I prefer the null approach, it’s more readable in that it keeps rightIcon aligned to the left with the other props…)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to change rightIcon conditionally in List Item. I am using ...
() => this.changeIcon(item) returns a function reference but ListItem expects a component. try. rightIcon={this.changeIcon(item)}.
Read more >
Conditionally Render Icon in List Item · Issue #715 - GitHub
Current behavior: I can't conditionally render icons in list items inside list. Expected behavior: Show icon on certain list items, and not on ......
Read more >
Version 2.3 Upgrade Guide - React Native Elements
ListItem has been somewhat of a kitchen sink. All the props can be replaced by inserting them as children in the order of...
Read more >
Components - NativeBase
Container comes with its predefined stylesheet, with an added advantage of ... Content, List, ListItem, Text, Left, Right, Icon } from 'native-base'; ...
Read more >
How to write a list literal with conditional items? [duplicate]
Don't think this is a duplicate because I'm not asking about adding items to a list. Rather, how to declare a list. Also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found