繁体   English   中英

React Leaflet:丑陋警告:列表中的每个孩子都应该有一个唯一的“关键”道具

[英]React Leaflet : ugly Warning: Each child in a list should have a unique "key" prop

我设置了一个传单地图组件,它遍历从数据库中获取的用户,其中每个用户都包含坐标。 开发工具扔给我:

警告:列表中的每个孩子都应该有一个唯一的“关键”道具。

我不是第一次收到此警告,我通常很容易找到解决方法,但这次我找不到解决方案。

一切正常,但这个警告一直在触发我

控制台警告: 控制台警告

我尝试将键作为标记组件标识符、弹出组件标识符,两者都是。

我看过一篇文章,建议将键作为我的地图函数的每个元素的片段标识符。 我尝试了一个 div 组件(对我来说似乎等于 Fragment?)仍然无法正常工作。

<MapContainer  id={!isMobile ? "browser-leaflet" : "mobile-leaflet"} center={POSITION} zoom={15} scrollWheelZoom={true}>
    <TileLayer
    attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png?lang=fr"
    />
    {contacts.map(contact => (
        <div key={contact.email}> <-- last try here
            <Marker //also tried here// position={[contact.lat, contact.lon]} icon={new Icon({iconUrl: contact.is_client ? markerIconClient : markerIconProspect, iconSize: [25, 41], iconAnchor: [12, 41]})}>
                <Popup //also tried here// offset={[0, -15]}>
                    <p style={{marginBottom: "1vh", textAlign: "center", fontSize: "1.5em"}}>
                        {contact.raison_sociale}
                    </p>
                    <Button variant="sub" className="mx-auto" style={{border: "teal", borderRadius: "15px", fontSize: "1em", display: "flex", justifyContent: "center"}}>
                        <a 
                        href={"https://www.waze.com/fr/live-map/directions?navigate=yes&to=ll." + contact.lat + "%2C" + contact.lon}
                        style={{textDecoration: "none", color: "black", backgroundColor: "#D0FCB3"}}
                        >
                            Allez-y
                        </a>
                    </Button>
                </Popup>
            </Marker>
        </div>
    ))}
    <SetMap />
</MapContainer>

错误似乎位于@Geolocalisation.jsx(我的整个页面组合)行:102,这是我根据客户端设备设置样式和导航栏的地方。

return ( <--- here l:102
        <>
            {!isMobile ? (
                [
                    <BrowserSidenav />, 
                    <style>
                        {browserLeafletContainer}
                    </style>
                ]
            ) : (
                <style>
                    {mobileLeafletContainer}
                </style>
            )}

有人知道如何解决吗? (PS:抱歉英语不好,不是母语,也没有使用在线翻译)

我的组件 Geolocalisation.jsx 的全部返回:(funcs 只是一个用户获取,一个 useEffect 删除没有纬度/经度的用户。

return (
        <>
            {!isMobile ? (
                [
                    <BrowserSidenav />, 
                    <style>
                        {browserLeafletContainer}
                    </style>
                ]
            ) : (
                <style>
                    {mobileLeafletContainer}
                </style>
            )}

            <MapContainer  id={!isMobile ? "browser-leaflet" : "mobile-leaflet"} center={POSITION} zoom={15} scrollWheelZoom={true}>
                <TileLayer
                attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
                url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png?lang=fr"
                />
                {contacts.map(contact => (
                    <div key={contact.email}>
                        <Marker position={[contact.lat, contact.lon]} icon={new Icon({iconUrl: contact.is_client ? markerIconClient : markerIconProspect, iconSize: [25, 41], iconAnchor: [12, 41]})}>
                            <Popup offset={[0, -15]}>
                                <p style={{marginBottom: "1vh", textAlign: "center", fontSize: "1.5em"}}>
                                    {contact.raison_sociale}
                                </p>
                                <Button variant="sub" className="mx-auto" style={{border: "teal", borderRadius: "15px", fontSize: "1em", display: "flex", justifyContent: "center"}}>
                                    <a 
                                    href={"https://www.waze.com/fr/live-map/directions?navigate=yes&to=ll." + contact.lat + "%2C" + contact.lon}
                                    style={{textDecoration: "none", color: "black", backgroundColor: "#D0FCB3"}}
                                    >
                                        Allez-y
                                    </a>
                                </Button>
                            </Popup>
                        </Marker>
                    </div>
                ))}
                <SetMap />
            </MapContainer>
        </>
    )

检查数据“联系人”中是否有相同的电子邮件。 确保 div 的每个键都是唯一的。

请记住,未定义也是一个键值。

如果您的数据库中有独特的电子邮件,但不是必需的,那么可能有几个人没有电子邮件。

我通常使用 _id/id 或 concider 使用多个字段来构建密钥。 你也可以做 key={contact.email ?? contact.phone} 或其他属性,如果电子邮件未定义...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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