I am working on an application where users can create posts and can mention other users
and use the #hashtag
in the post. Through API call I get the list of post, now the question is how can we wrap hashtags and usernames with next.js Link
component so that on clicking on these link user can be navigated to different pages without page refresh.
I am able to wrap this with <a>
tag and render the text with dangerouslySetInnerHTML
. But this way my page will get refreshed by clicking on the link I don’t want off course this behavior.
Here’s my code to format the text with <a>
tag:
export const formatPostText = (text) => {
let splitText = text.split(' ')
splitText.forEach((word, index) => {
if (word[0] === "@") {
splitText[index] = '<a class="text-cyanBlue" href="/' + word.replace('@', '') + '/in' + '">' + word + '</a>'
} else if (word[0] === "#") {
splitText[index] = '<a class="text-cyanBlue" href="hashtag' + '/' + word.replace('#', '') + '">' + word + '</a>'
}
})
return splitText.join(' ')
}
I do want the same behavior as LinkedIn and Facebook has done it.
Approach: One way I can think of is to use React.createElement but I doubt this is gonna work.
Thanks in advance!!
2
Answers
I used spans with colors here to make example clear:
https://codesandbox.io/s/modest-http-wnp5xq?file=/src/App.js
I implemented similar things in a recent project. I hope this might be helpful.