简体   繁体   English

如何在 useState 中呈现链接 - nextjs

[英]How to render a Link in useState - nextjs

I am trying to render a link, which I am defining in a useState previously.我正在尝试呈现一个链接,我之前在 useState 中定义了该链接。 Here is the code:这是代码:

export const CheckEntry = ({ id, zala }) => {
const [message, setMessage] = useState('');
const [title, setTitle] = useState('');
const [faIcon, setFaIcon] = useState('');
const [iconColor, setIconColor] = useState('');
const [usr, setUsr] = useState();

useEffect(() => {
    if (!usr) {
        return null;
    }
    else {
        if (data.user.membershipLocation != zala) {
            setTitle("Access denied")
            setFaIcon(faSquareXmark)
            setIconColor("red");
            setMessage(`You don't have a membership for this training center. You can purchase ${<Link href="/buy-membership"><a>here</a></Link>}.`)
        }}
    }, [usr])
    if (message != '') {
        return (
            <div>
                <section className={styles.card}>
                    <h4 className={styles.sectionTitle}>{title}</h4>
                    <Spacer size={0.5} axis="vertical" />
                    <FontAwesomeIcon
                        icon={faIcon}
                        style={{ fontSize: 100, color: iconColor }}
                    />
                    <Spacer size={0.5} axis="vertical" />
                    <h3 className={styles.sectionMessage}>{message}</h3>
                </section>
            </div>
        )
    }
    else {
        return null;
    }
}

However, when it renders, it shows this:但是,当它渲染时,它会显示: 在此处输入图像描述

How do I successfully render this Link component?如何成功渲染此 Link 组件?

This happen because your message is a string and not a jsx.发生这种情况是因为您的message是字符串而不是 jsx。

If you want to render elments use jsx like this:如果要渲染元素,请像这样使用 jsx:

setMessage(
  <>
    You don't have a membership for this training center.
    You can purchase 
    <Link href="/buy-membership"><a>here</a></Link>
    .
  <>
)

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

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