Reactjs 如何添加未关闭的JSX标记?
我有以下代码:Reactjs 如何添加未关闭的JSX标记?,reactjs,Reactjs,我有以下代码: {props.href && ( <Link href={props.href}> <button> {props.children} </button> </Link> )} {!props.href && ( <button> {props.children} </button
{props.href && (
<Link href={props.href}>
<button>
{props.children}
</button>
</Link>
)}
{!props.href && (
<button>
{props.children}
</button>
)}
{props.href&&(
{props.children}
)}
{!props.href&&(
{props.children}
)}
我如何将其简化为以下内容:
{props.href && ( <Link href={props.href}> )}
<button>
{props.children}
</button>
{props.href && ( </Link> )}
{props.href&&()}
{props.children}
{props.href&&()}
这段代码出现语法错误,因为我打开链接标记时没有关闭它。您必须关闭JSX中打开的标记!您可以使用三元运算符来简化代码,例如
{props.href ? (
<Link href={props.href}>
<button>
{props.children}
</button>
</Link>) : (
<button>
{props.children}
</button>
)}
{props.href(
{props.children}
) : (
{props.children}
)}