简体   繁体   English

JSX中的条件链接

[英]Conditional link in JSX

I'm trying to write a way to render links conditionally. 我正在尝试写一种有条件地呈现链接的方法。 I have the following function: 我有以下功能:

const renderLinkIf = (content, condition, href) => {
  if (condition) {
    return (<Link to={href}>{content}</Link>);
  }
  return (content);
};

With very simple tasks it works: 通过非常简单的任务,它可以工作:

{ renderLinkIf('test', true, '/dashboard') }

However, I can't figure out how to render more complex contents: 但是,我不知道如何呈现更复杂的内容:

{renderLinkIf(
  <span className={sectionCompleted(30) ? 'completed' : null}>
    {sectionCompleted(30) ? <CheckIcon /> : <HeaderPersonalInfo />}
  </span> Personal Info,
  true,
  '/dashboard',
)}

I just get Syntax Errors. 我刚收到语法错误。

How can I pass more complex JSX through renderLinkIf to be rendered? 如何通过renderLinkIf传递更复杂的JSX以进行渲染?

I believe you'd have to wrap the <span> and the text Personal Info in a single element for react. 我相信您必须将<span> 个人信息 ”文本包装在一个元素中才能做出反应。 Other than that, I don't see any obvious errors: 除此之外,我看不到任何明显的错误:

{renderLinkIf(
  <span><span className={sectionCompleted(30) ? 'completed' : null}>
    {sectionCompleted(30) ? <CheckIcon /> : <HeaderPersonalInfo />}
  </span> Personal Info</span>,
  true,
  '/dashboard',
)}

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

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