如何在文本中将检测到的URL转换为React中的链接?
我正在制作一个SNS网站,并尝试将文本中检测到的URL转换为显示为链接。
import React from "react";
import "./PostCard.css";
import { useNavigate } from "react-router-dom";
import datePretty from "../../helperFunctions/datePretty";
function PostCard({ userName, postTime, likeCount, commentCount, postText }) {
var pastTime = datePretty(postTime)
return (
<div className="postingCard">
<div className="postingHeader">
<div className="postingProfile">
<img className="postPP" src="profile.png" alt="Sample profile"></img>
</div>
<div className="postingWriter">
<h3>{userName}</h3>
</div>
<div className="dateWritten">
<h4>Posted {pastTime} ago</h4>
</div>
<div className="postSetting">
<i className="fa-solid fa-ellipsis fa-2xl" id="postSet"></i>
</div>
</div>
<div className="postingBody">
<div className="postingSection">
<div className="postWords">
<h4>{postText}</h4>
</div>
</div>
<div className="iconSection">
<div className="likeSection">
<i className="fa-regular fa-thumbs-up fa-2xl" id="like"></i>
<div className="likeCount" id="likeNum">
{likeCount}
</div>
</div>
<div className="commentSection">
<i className="fa-regular fa-comment-dots fa-2xl"></i>
<h5 className="commentCount">{commentCount}</h5>
</div>
</div>
</div>
</div>
);
}
export default PostCard;
这是我目前正在处理的代码。 posttext 是我尝试将其转换为链接显示的元素。
例如,当我像“ Hello,https://www.youtube.com”上上传时,然后 https:// www。 youtube.com 应该可以单击并重定向到YouTube。
您能告诉我如何解决这个问题吗?
I am making a SNS website and trying to convert detected url in text to shown as links.
import React from "react";
import "./PostCard.css";
import { useNavigate } from "react-router-dom";
import datePretty from "../../helperFunctions/datePretty";
function PostCard({ userName, postTime, likeCount, commentCount, postText }) {
var pastTime = datePretty(postTime)
return (
<div className="postingCard">
<div className="postingHeader">
<div className="postingProfile">
<img className="postPP" src="profile.png" alt="Sample profile"></img>
</div>
<div className="postingWriter">
<h3>{userName}</h3>
</div>
<div className="dateWritten">
<h4>Posted {pastTime} ago</h4>
</div>
<div className="postSetting">
<i className="fa-solid fa-ellipsis fa-2xl" id="postSet"></i>
</div>
</div>
<div className="postingBody">
<div className="postingSection">
<div className="postWords">
<h4>{postText}</h4>
</div>
</div>
<div className="iconSection">
<div className="likeSection">
<i className="fa-regular fa-thumbs-up fa-2xl" id="like"></i>
<div className="likeCount" id="likeNum">
{likeCount}
</div>
</div>
<div className="commentSection">
<i className="fa-regular fa-comment-dots fa-2xl"></i>
<h5 className="commentCount">{commentCount}</h5>
</div>
</div>
</div>
</div>
);
}
export default PostCard;
This is my code that I currently working on. The postText is the element that I tried to convert to shown as link.
For example, when I uploaded like "hello, https://www.youtube.com", then https://www.youtube.com should be able to click and redirect to YouTube.
Can you please tell me how to figure this out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样做的反应方式是创建一个反应组件并将文本作为道具传递,并处理该组件内部的逻辑。
这是我对此的看法,您应该将链接检测更改为您的需求。
The react way of doing this would be to create a react component and pass the text as props, and handle the logic inside that component.
Here is my take on this, you should change the link detection to your needs.
假设已经在原始字符串中检测到URL的逻辑已经构建,并且您在后文本中传递的内容是一个包含URL的字符串,则可以将锚标记在H4上包裹,然后只需将HREF属性设置为{postText}}
如果您打算在新选项卡中打开它
Assuming that the logic to detect a url in the original string is already built and that what you are passing down in postText is a string containing a url, you can wrap an anchor tag around your h4 and simply set the href property to {PostText}
If you intend to have it open in a new tab make sure to set target to _blank and rel to noreferrer