Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何添加未关闭的JSX标记?_Reactjs - Fatal编程技术网

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}
)}