如何解决在react中的特定列表项中添加类/从中删除类
我有一个react页面,该页面呈现来自本地数据库中的数组的链接列表。当我将鼠标悬停在每个链接(列表项)上时,我想显示链接提示。
当前,当我将鼠标悬停在列表项(网页上的链接)上时,会显示来自其他链接的所有链接提示。我只希望将鼠标悬停在上面以显示提示。我怎样才能达到这种效果?
-
发生的问题:
-
反应代码:
function Works() {
const [hoverHint,setHoverHint] = useState(false);
function showHoverHint() {
setHoverHint(true);
}
function hideHoverHint() {
setHoverHint(false);
}
const linkMaps = {
website: <Globe fill="#aaa" size={20} />,github: <GitHub fill="#aaa" size={20} />,gitlab: <GitLab fill="#aaa" size={20} />,apk: <Android fill="#aaa" size={20} />,youtube: <YouTube fill="#aaa" size={20} />
};
return(
<ul className="work-links">
{work.links.map(link => (
<li>
<span
className={
hoverHint ? 'before show-hint' : 'before hide-hint'
}
>
{link.text}
</span>
<a
href={link.url}
target="_blank"
rel="noopener noreferrer"
onMouseOver={showHoverHint}
onMouseOut={hideHoverHint}
>
{linkMaps[link.type]}
</a>
<span
className={
hoverHint ? 'after show-hint' : 'after hide-hint'
}
>
{link.text}
</span>
</li>
))}
</ul>
);
}
CSS代码:
.work-links {
list-style-type: none;
padding: 0;
margin-top: 0.3em;
margin-bottom: 0;
position: absolute;
display: flex;
flex-direction: column;
justify-content: flex-start;
}
.work-links svg {
transition: 0.15s;
}
.work-links a {
display: inline-block;
padding: 0.4em 0.4em 0;
margin-bottom: 0.8em;
background-color: #eee;
border-radius: 50%;
transition: 0.15s;
z-index: 1;
}
.work-links a:hover {
background-color: #333;
}
.work-links a:hover svg {
fill: #eee;
}
.work-card-container:nth-child(odd) .work-links {
right: -11em;
align-items: flex-start;
}
.work-card-container:nth-child(even) .work-links {
left: -11em;
align-items: flex-end;
}
.work-card-container:nth-child(odd) .before {
display: none;
}
.work-card-container:nth-child(even) .before {
right: 0.5em;
transition: 0.1s;
}
.work-card-container:nth-child(even) .after {
display: none;
}
.work-card-container:nth-child(odd) .after {
left: 0.5em;
transition: 0.2s;
}
.hide-hint {
opacity: 0;
}
.work-card-container:nth-child(even) .hide-hint {
transform: translateX(50%);
}
.work-card-container:nth-child(odd) .hide-hint {
transform: translateX(-50%);
}
.show-hint {
transform: translateX(0);
opacity: 1;
}
.work-links > li {
display: flex;
align-items: center;
}
.work-links span {
position: relative;
top: -0.5em;
color: #888;
}
解决方法
您需要基于唯一标识符而不是共享状态值来触发悬停状态。
这是我想到的一种方法:
const [selectedLink,setSelectedLink] = React.useState(null)
<ul className="work-links">
{work.links.map(link => (
<li>
<span
className={
hoverHint ? 'before show-hint' : 'before hide-hint'
}
>
{link.text}
</span>
<a
href={link.url}
target="_blank"
rel="noopener noreferrer"
// Use the URL as a unique identifier
onMouseOver={() => setSelectedLink(link.url)}
// Set to null on mouseout
onMouseOut={() => setSelectedLink(null)}
>
{linkMaps[link.type]}
</a>
<span
className={
selectedLink === link.url ? 'after show-hint' : 'after hide-hint'
}
>
{link.text}
</span>
</li>
))}
</ul>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。