首页 > 解决方案 > 这个无效的正则表达式标志错误是什么?

问题描述

我有条件地在我的 React 应用程序中渲染一个 div。这个...

    if (props.trades.length > 0)
    return <div class="header">Your trades:</div>
    else
    return <div class="header">You haven't made any trades.</div>

有效,但我想像这样在第二个选项中包含一个链接

    if (props.trades.length > 0)
    return <div class="header">Your trades:</div>
    else
    return <div class="header">You haven't made any trades. Click <a to={{/page}}>here</a> to make a trade.</div>

但是我在关闭标签时收到此错误:“解析错误:无效的正则表达式标志”。

为什么是这样?

标签: htmlreactjs

解决方案


发生这种情况是因为您应该使用to这种方式:

注意:如果您使用的是 html a tag,您可能希望更改tohref

<a to="/page">here</a>
// or 
const to = "/page"
...
<a to={to}>here</a>

推荐阅读