Skip to content

Instantly share code, notes, and snippets.

@VivekSaha
Created September 2, 2025 06:29
Show Gist options
  • Save VivekSaha/e423c0c3ca318e4c9fe183d7c26ce90f to your computer and use it in GitHub Desktop.
Save VivekSaha/e423c0c3ca318e4c9fe183d7c26ce90f to your computer and use it in GitHub Desktop.
React - useState Multiple Condition using Ternary Operator
import { useState } from "react"
const App = () => {
const [counter, setCounter] = useState(0);
const handleClick = () => {
setCounter(counter + 1);
}
return(
<>
<h1>useState Multiple Condition using Ternary Operator</h1>
{
counter == 0 ? <p>Condition 1</p>
: counter == 1 ? <p>Condition 2</p>
: counter == 2 ? <p>Condition 3</p>
: <p>Out of Range</p>
}
<button onClick={handleClick} disabled={counter > 2}>Click Here</button>
</>
)
}
export default App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment