Reputation: 194
I would like to use a value of a state variable in a JSX expression and cannot figure out the syntax. Here is simplified code:
function App() {
const [isLoading, setIsLoading] = useState(false);
const [page, setPage] = useState(1);
return (
<div>
{isLoading ? `Loading...` : 'Load Page ' + {page}}
</div>
);
}
export default App;
If isLoading
is false, I would like the output to say Load Page 1.
As of now it says Load Page [object Object] and I am stuck on the syntax.
Thanks.
Upvotes: 0
Views: 62
Reputation: 2955
Try this
function App() {
const [isLoading, setIsLoading] = useState(false);
const [page, setPage] = useState(1);
return <div>{isLoading ? `Loading...` : `Load Page ${page + 1}`}</div>;
}
export default App;
Upvotes: 2
Reputation: 24661
{isLoading ? `Loading...` : 'Load Page ' + page}
stop adding {}
everywhere
Upvotes: 2
Reputation: 41417
try this
{isLoading ? `Loading...` :`Load Page ${page}`}
Upvotes: 2