admin管理员组文章数量:1415460
Reactjs How to write the greater than 0 in jsx?
this is my current code
return (
{num > 0 ? (
console.log('greater than 0')
):(console.log('less than zero')}
)
Reactjs How to write the greater than 0 in jsx?
this is my current code
return (
{num > 0 ? (
console.log('greater than 0')
):(console.log('less than zero')}
)
Share
Improve this question
asked Jan 26, 2022 at 9:30
user15404864user15404864
3
-
1
May be try enclosing in a JSX tag like so:
return (num > 0 ? <div>greater than 0</div> : <div>less than zero</div>);
rather thanconsole.log
. – jsN00b Commented Jan 26, 2022 at 9:37 -
1
return <>{num > 0 ? <div>greater</div> : <div>less than</div>}</>
definitely works. – Wiktor Zychla Commented Jan 26, 2022 at 9:41 -
Try to remove the
console.log
call and just leave the string... – GACy20 Commented Jan 26, 2022 at 9:43
4 Answers
Reset to default 2 export default function App() {
const num = 2;
return (
<div>
{num > 0 ? <h1> Greater than Zero </h1> : <h1> Zero</h1>}
</div>
);
}
You mean you wanna display the evaluation on screen? There is a clean way to do that.
const App = () => {
...// logic you want
const ment = num > 0 ? 'greater than 0' : 'less than zero';
return <p>{ment}</p>
}
Replace >
by >
and replace <
by <
You can use the following code as a replacement :
return (
{num > 0 ? (
console.log('greater than 0')
):(console.log('less than zero')}
)
We have to first validate num exists and then do the conditional checking if it exists.
return (
{num && num > 0 ? (
console.log('greater than 0')
):(console.log('less than zero'))})
Also you were missing a ')' at the end, which was a syntactical error
本文标签: javascriptReactjs How to write the greater than 0 in jsxStack Overflow
版权声明:本文标题:javascript - Reactjs How to write the greater than 0 in jsx? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745177076a2646288.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论