Switch Statements in JSX

JSX allows us to describe our UI using javascript expressions. This has interesting implications for control flow, because control flow statements in javascript (if, for, switch, etc) do not return values (they are imperative). This means that while we…


This content originally appeared on DEV Community and was authored by David Woodward

JSX allows us to describe our UI using javascript expressions. This has interesting implications for control flow, because control flow statements in javascript (if, for, switch, etc) do not return values (they are imperative). This means that while we can use control flow statements to return top-level blocks of JSX,

if (isLoading) {
  return <span>loading...</span>
} else {
  return (
    <section>
      <h1>My content</h1>
      {content}
    </section>
  )
}

we cannot use them inline.

return (
  <section>
    <h1>My content</h1>
    {/* this is a syntax error ... */}
    {if (isLoading) {
      return <span>loading</span>
    } else {
      return content
    }}
  </section>
)

However, since JSX allows for embedded Javascript expressions, rather than statements, we can mimic the functionality of if, else and switch using the ternary operator!

// a simple if-else
return (
  <section>
    <h1>My content</h1>
    {isLoading ? (
      <span>loading</span>
    ) : (
      content
    )}
  </section>
)

// a switch (nested ternary)
return (
  <section>
    <h1>My content</h1>
    {isLoading ? (
      <span>loading</span>
    ) : hasErrors ? (
      <span>something went wrong</span>
    ) : (
      content // this is the last "else" case
    )}
  </section>)

Scrupulous style guide adherents may claim that nested ternaries are confusing and error prone, but functionally it's not different then an if-else-if chain. If you and your linter can get used to it, I think it's a cleaner way to represent switch logic in JSX!


This content originally appeared on DEV Community and was authored by David Woodward


Print Share Comment Cite Upload Translate Updates
APA

David Woodward | Sciencx (2021-07-21T00:06:21+00:00) Switch Statements in JSX. Retrieved from https://www.scien.cx/2021/07/21/switch-statements-in-jsx/

MLA
" » Switch Statements in JSX." David Woodward | Sciencx - Wednesday July 21, 2021, https://www.scien.cx/2021/07/21/switch-statements-in-jsx/
HARVARD
David Woodward | Sciencx Wednesday July 21, 2021 » Switch Statements in JSX., viewed ,<https://www.scien.cx/2021/07/21/switch-statements-in-jsx/>
VANCOUVER
David Woodward | Sciencx - » Switch Statements in JSX. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/21/switch-statements-in-jsx/
CHICAGO
" » Switch Statements in JSX." David Woodward | Sciencx - Accessed . https://www.scien.cx/2021/07/21/switch-statements-in-jsx/
IEEE
" » Switch Statements in JSX." David Woodward | Sciencx [Online]. Available: https://www.scien.cx/2021/07/21/switch-statements-in-jsx/. [Accessed: ]
rf:citation
» Switch Statements in JSX | David Woodward | Sciencx | https://www.scien.cx/2021/07/21/switch-statements-in-jsx/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.