首页 > 解决方案 > 为什么工具提示被渲染两次?

问题描述

出于某种原因,react-tooltip 正在渲染 2 个工具提示,当我只要求它渲染一个(当然)时,这是我的一个 react 组件的示例:

布鲁赫

但是,它不应该,因为代码只呈现一次。让我给你看一下反应代码:

import React from "react";
import ReactTooltip from "react-tooltip";
import "./StaffMembers.scss";

import ProfileIcon from "../public/profile.svg";

function getKeysAndValues<T>(item: T[]) {
    return Array.from(item.entries());
}

export interface StaffDetails {
    pfp: string;
    email: string;
}

const StaffMembers: React.FC<{ staff: StaffDetails[] }> = (props) => {
    return (
        <>
            <div className="staffMembers">
                <h1 className="title">All Staff Members</h1>
                <div className="staffTable">
                    {getKeysAndValues(props.staff).map(([id, staff]) => (
                        <div className="staffMember" key={id}>
                            <div className="userDetail">
                                <img
                                    className="pfp"
                                    src={staff.pfp}
                                    alt="pfp"
                                />
                                <p className="email" data-tip={staff.email}>
                                    {staff.email}
                                </p>
                            </div>
                            <button className="copyBtn">Copy UID</button>
                            <button className="logoutBtn">Log-out</button>
                        </div>
                    ))}
                </div>
            </div>
            <ReactTooltip />
        </>
    );
};

export default StaffMembers;

以及该组件的 SCSS:

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;400;500&display=swap');

.staffMembers::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
    border-radius: 10px;
    background-color: #F5F5F5;
}

.staffMembers::-webkit-scrollbar {
    width: 12px;
    background-color: #F5F5F5;
}

.staffMembers::-webkit-scrollbar-thumb {
    border-radius: 10px;
    -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, .3);
    background-color: #555;
}

.staffMembers {
    background: #FEFEFF;
    border: 3px solid rgba(217, 217, 217, 0.5);
    box-sizing: border-box;
    border-radius: 10px;

    width: 450px;

    display: flex;
    flex-direction: column;
    align-items: center;

    .title {
        font-family: Poppins;
        font-style: normal;
        font-weight: normal;
        font-size: 24px;
        line-height: 36px;
        text-align: center;
        padding: 20px;

        color: #000000;
    }

    .staffTable {
        width: 100%;
        border-top: 1px solid rgba(217, 217, 217, 0.5);
        border-collapse: collapse;

        display: flex;
        flex-direction: column;


        max-height: 350px;
        display: block;
        overflow-y: auto;
        white-space: nowrap;
    }

    .staffMember {
        border-bottom: 1px solid rgba(217, 217, 217, 0.5);
        display: flex;
        align-items: center;
        justify-content: space-around;
    }

    .userDetail {
        font-family: Poppins;
        font-style: normal;
        font-weight: normal;
        font-size: 14px;
        display: flex;
        align-items: center;
        flex-direction: row;
    }

    .pfp {
        height: 41px;
    }

    .email {
        margin-left: 15px;
        width: 150px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    .copyBtn {
        border: 0;
        background: #7EDD7E;
        border-radius: 5px;
        font-family: Poppins;
        font-style: normal;
        font-weight: normal;
        font-size: 12px;
        line-height: 18px;
        /* identical to box height */

        text-align: center;

        color: #000000;
        height: 25px;
    }

    .logoutBtn {
        border: 0;
        background: #FA2C2C;
        border-radius: 5px;
        font-family: Poppins;
        font-style: normal;
        font-weight: normal;
        font-size: 12px;
        line-height: 18px;
        /* identical to box height */

        text-align: center;

        color: #000000;
        height: 25px;
    }
}

我无法弄清楚到底出了什么问题——因为我只在<p>标签上调用了一次 data-tip 道具。

谢谢您的帮助。

标签: javascriptcssreactjstypescriptsass

解决方案


推荐阅读