Skip to content

Instantly share code, notes, and snippets.

@lejonmanen
Last active February 29, 2024 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lejonmanen/f46f43bfb13181222825fc0afcd9cdfc to your computer and use it in GitHub Desktop.
Save lejonmanen/f46f43bfb13181222825fc0afcd9cdfc to your computer and use it in GitHub Desktop.
Conditional rendering exercise
/*
The Budget component uses "conditional rendering" to do
different things based on its props. What will each line
in the App component render?
*/
const App = () => (
<main>
<Budget value={5500} />
<Budget value={240} />
<Budget value={0} />
<Budget value={-150} />
<Budget value={120} />
</main>
)
// Assume that there are CSS classes named "low" and "high" defined in App.css.
const Budget = (props) => {
let css = '', display = props.value
if( props.value < 0 ) {
css = 'low'
}
else if( props.value >= 1000 ) {
css = 'high'
}
else if( props.value === 0 ) {
display = 'Empty!'
}
return (
<span className={css}> {display} </span>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment