简体   繁体   中英

Inserting Next.js Links into an html string

I have an html string that I retrieve from an API.

const htmlString = "<p>Hello World</p>";

I'm using react's dangerouslySetInnerHTML to display that content on my page.

Now before displaying it, I'd like to process it and put a link on " World ". But not an <a> Tag. I need an actual Next.js <Link> tag. So a string replace probably won't do the job.

The result I want to achieve in jsx is:

const processed = <p>Hello <Link href="/my-route"><a>World</a></Link></p>

I've thought of using React.createElement but I'm not sure how to interpolate the content inside the string.

Any ideas how I could achieve this?

 Hello<Link href="https://github.com">World</Link>

You dont need to specify another anchor tag inside a link. This should work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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