JSX inside ternary not returning desired result

Tim Bogdanov

The problem:

I am trying to return an icon along with text if a ternary is true, and just text if ternary is false. Here is my code:

{quote.is_archived ? <PencilFill className="mr-10" size={ 10 }/>  + 'View' : 'Edit'}

When I do that, this is what I get, can someone point me to why this is happening.

enter image description here

Nicholas Tower

<PencilFill className="mr-10" size={ 10 }/> + 'View'

The first part of this is an object, and the second part is a string, so when you concatenate them together you get [object Object]View. What you probably meant to do was a Fragment, as in:

<React.Fragment>
  <PencilFill className="mr-10" size={ 10 }/>
  View
</React.Fragment>

Or using the shorthand notation:

<>
  <PencilFill className="mr-10" size={ 10 }/>
  View
</>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

If statement not returning the desired result

Excel "IF" "AND" statement not returning desired result

Query not returning desired ordered result

Asking for input until desired result, then returning result

.map() function inside ternary in react native jsx

map is not a function returning div inside ternary operator

How to use ternary operator for returning jsx using react and typescript?

Function inside JSX, is not returning the elements: ReactJS

How to have two function calls inside ternary operator in JSX?

return two elements inside the first part of ternary operator in JSX

Function to calculate max value of int not returning desired result

Struts 2 action not returning desired result to Jquery Ajax call

Pandas ix method returning blank dataframe instead of desired result

NodeJS Selenium-webdriver custom wait method not returning desired result

MySQL left join with LIMIT 1 not returning desired result

JavaScript function containing array methods not returning desired result

Suspending until StateFlow reaches one of the desired states and returning the result

Why does returning inside the ternary operator throw a SyntaxError?

How to write case expression inside merge statement to print the desired result

How can I filter the result of a ternary that's inside a map?

Error while conditional rendering of state value using ternary operator inside jsx in Reactjs 18.0

Closure not returning the desired output

Function not returning desired string

Ternary operator is not returning undefined

Why does returning null (where a boolean is expected) as the result of a ternary operator compile?

multiple condition in ternary operator in jsx

Ternary Operator in React/JSX Not Working

Why is ternary operator in jsx not working

How to use ternary expressions by JSX?