简体   繁体   English

如果不是jsx使用 <Link> 反应

[英]If else jsx using <Link> in react

render(){
    return(
        {startDate && endDate ? <Link to=`/event/${event_id}?start=${startDate}&?end=${endDate}` target="_blank"> : <Link to=`/event/${event_id}` target="_blank">
            <div>lots of my content here</div>
        </Link>
    )
}

This code might be easy to understand but it won't work, it has snytax error. 该代码可能易于理解,但无法正常工作,但存在snytax错误。 I can use onClick because I want to open the link in a new tab. 我可以使用onClick,因为我想在新标签页中打开链接。 How to use if else with Link ? 如何在Link上使用if else? Any clue? 有什么线索吗?

You have a few mistakes 你有一些错误

  1. return should return only one component return应该只返回一个分量
  2. You have syntax error as parenthesis are not matching 您有语法错误,因为括号不匹配

See here 看这里

render() {
    return (
        <div>
          {
           startDate && endDate
           ?
           <Link to=`/event/${event_id}?start=${startDate}&?end=${endDate}` target="_blank"/> 
           :
           <Link to=`/event/${event_id}` target="_blank"/>
          }
          <div>lots of my content here</div>
     </div>
    )
}
render(){
   let link = `/event/${event_id}`;
   link = (startDate && endDate) ? `${link}?start=${startDate}&?end=${endDate}`;

    return(
        <Link to={link} target="_blank"> 
            <div>lots of my content here</div>
        </Link>
    )
}

Wrap you Link to statement within {} and also you neeed to close the first Link tag too as well as end the ternary operator exression with } {}中将Link换成语句,然后也需要关闭第一个Link标记,并用}结束三元运算符

render(){
    return(
        {startDate && endDate ? <Link to={`/event/${event_id}?start=${startDate}&?end=${endDate}`} target="_blank"><div>lots of my content here</div></Link> : <Link to={`/event/${event_id}`} target="_blank">
            <div>lots of my content here</div>
        </Link>
       }
    )
}

However if the Link tags have the same content. 但是,如果链接标签具有相同的内容。 It makes more sense to have ternary expression on the link target only 仅在链接目标上具有三元表达式会更有意义

render(){
        var link = (startDate && endDate) ? `/event/${event_id}?start=${startDate}&?end=${endDate}` : `/event/${event_id}`;

        return(
            <Link to={link} target="_blank">
                <div>lots of my content here</div>
            </Link>
           }
        )
    }

I would do it like this, 我会这样

render() {
  const linkTo = startDate && endDate
    ? `/event/${event_id}?start=${startDate}&?end=${endDate}`
    : `/event/${event_id}`;

  return (
    <Link to={linkTo} target="_blank">
      <div>lots of my content here</div>
    </Link>
  );
}

You have the problem, because you can't use if-else for halves of react-components, but only for whole components or their internal parts. 您有问题,因为您不能将if-else用于一半的react-components,而只能用于整个组件或其内部零件。

It means that you can't use such code: 这意味着您不能使用以下代码:

 {condition ? <Link to="1"> : <Link to="2"> <div>lots of my content here</div> </Link>} 

and instead you could use such variant: 相反,您可以使用以下变体:

 const content = <div>lots of my content here</div> {condition ? <Link to="1">{content}</Link> : <Link to="2">{content}</Link/>} 

or you can use condition for to parameter, as was proposed in other answers 或者您可以使用条件to参数,在其他的答案提出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM