Skip to content

Instantly share code, notes, and snippets.

@richleach
Last active April 16, 2020 02:24
Show Gist options
  • Save richleach/bd26bc324f7e7c4776890283b4e24ff7 to your computer and use it in GitHub Desktop.
Save richleach/bd26bc324f7e7c4776890283b4e24ff7 to your computer and use it in GitHub Desktop.
REACT: Basic use of ternary conditional operators in React. This code is from the same component, but commented based on if the code is in the function() above the returned JSX, or actually within the JSX.
//this is basic ternary JS inside a function block but outside of the return(...jsx code...) block
//if regularMarketChange is LT 0 then set red, else green
{response.data.price.regularMarketChange.fmt < 0 ? setQuoteColor('red') : setQuoteColor('green') };
//this is ternary JS inside JSX
//Notice the implementation of the quoteColor variable (set above) in the Nav tag below
//If the value in the marketChange variable is GT 0 then display +, else display - (notice the quotes!)
<Nav
className="nav-tabs-neutral"
role="tablist"
tabs style={{ backgroundColor: quoteColor }}>
<Col md="5">
<h2 style={{ color: "white", marginBottom: "-2px" }}><strong>${price}</strong></h2>
<h6 className="category" style={{ color: "white", marginBottom: "-5px", textAlign: "left" }}>
{marketChange > 0 ? '+' : '-'}{marketChangePercent} {marketChange > 0 ? '+' : ''}{marketChange}({currency})
Volume: {marketVolume}
</h6>
</Col>
</Nav>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment