如果其他条件在ReactJ中使用变量,如何使用
我正在ReactJs工作。现在,我获得了当前的url/hostName
。现在,我想使用此URL具有IF-ELSE条件,这意味着如果url =“/”
(主页),我只想显示第一个标头,则应显示第二个单词。换句话说,我想知道如何将IF-ELSE条件与动态变量一起使用? 这是我当前的代码
import { useRouter } from 'next/router'
const Header = () => {
const router = useRouter();
const url =router.asPath;
return (
<>
<div>
//need to use if else condition based on "url" variable
</div>
</>
)
I am working in Reactjs. Right now I am getting the current url/hostname
. Now I want to use this URL with an if-else condition, which means I just want if url="/"
(home page) then the first header should display otherwise second word should display. In other words, I want to know how we can use if-else condition with dynamic variable?
Here is my current code
import { useRouter } from 'next/router'
const Header = () => {
const router = useRouter();
const url =router.asPath;
return (
<>
<div>
//need to use if else condition based on "url" variable
</div>
</>
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在此处使用
三元运算符
来渲染,例如:You can use
ternary operators
there to render based on a condition, like this:您使用两种方法
You use both methods