Reputation: 1
Created a state with a default value of null
const [zero, setZero] = useState(null)
If we set the state, via setZero(0)
, to 0
, the jsx is not showing 0
.
In jsx:
{zero ? zero : "No data"}
It gives "No data" as a output.
How to show zero value 0
in jsx?
Upvotes: 0
Views: 622
Reputation: 370679
Zero is falsy, so zero ?
will result in the alternative expression being used, the No data
. Use the nullish coalescing operator instead.
{ zero ?? "No data" }
which evaluates to the right-hand side if the left side is undefined
or null
.
Upvotes: 3