gpt4 book ai didi

javascript - 警告 : React does not recognize the `viewState` prop on a DOM element

转载 作者:行者123 更新时间:2023-12-03 13:45:27 25 4
gpt4 key购买 nike

我操纵组件的状态并使用状态中的对象(输入参数)来显示一些信息。

const renderTooltip = ({ object: hoveredObject, x: pointerX, y: pointerY }) => {
// console.log(hoveredObject);
const {points} = hoveredObject;

const calLoadTime = () => {
const rawTime = points.reduce(
(accumulator, currentValue) =>
currentValue ? accumulator + currentValue.SPACES : accumulator,
0
) / points.length;

return rawTime.toFixed(1);
}

return (
<span
style={{
lineHeight: '20px',
borderRadius: '3px',
width: '200px',
left: pointerX+10,
top: pointerY+5,
}}
>
{`Length: ${points.length}\n`}
</span>
)
}

但是收到以下警告,我检查了我没有操作 DOM 元素上的状态,或者有一些名为 viewState 的属性?这个警告是什么意思?

Warning: React does not recognize the viewState prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase viewstate instead. If you accidentally passed it from a parent component, remove it from the DOM element.

我的组件的完整代码如下所示:

import React, { memo, useEffect, useRef, useState } from 'react';
import DeckGL, {HexagonLayer} from 'deck.gl';
import { StaticMap } from 'react-map-gl';

import { INITIAL_VIEW_STATE, LAYER_CONFIGS } from './configs';
import { addMapControl } from './tools';
import { MAPBOX_TOKEN } from './constants';
import styles from './index.less';

const Map = (props) => {
const {
controller = true,
baseMap = true,
initialViewState = INITIAL_VIEW_STATE,
layerType = '3d-heatmap',
renderTooltip = DEFAULT_RENDER_TOOLTIP,
} = props;

const [tooltip, setTooltip] = useState({
object: null,
// eslint-disable-next-line react/no-unused-state
x: 0,
// eslint-disable-next-line react/no-unused-state
y: 0,
})

/**
* layers render function
*/
const renderLayers = () => {
const {
data,
accData = [],
accSpeed = 0.1,
accRadiusScale = 4,
accRadiusMaxPixels = 200,
accColorRange,
coverage = 1,
radius,
elevationScale,
...otherProps
} = props;
const layers = [];

layers.push(
new HexagonLayer({
...LAYER_CONFIGS.AugmentHexagonLayer,
...otherProps,
coverage,
data,
radius,
onHover: setTooltip,
}),
);

return layers;
}

/**
* add Control for language switching
* @param {*} event
*/
const addControlHandler = (event) => {
const map = event && event.target;
if (map) {
addMapControl(map);
}
};

const { object } = tooltip || {};

return (
<DeckGL
width="100%"
height="100%"
layers={renderLayers()} // eslint-disable-line
initialViewState={initialViewState}
controller={controller}
>
{baseMap && (
<StaticMap
onLoad={addControlHandler}
reuseMaps
mapStyle="mapbox://styles/mapbox/dark-v9"
preventStyleDiffing
mapboxApiAccessToken={MAPBOX_TOKEN}
/>
)}
{object && renderTooltip(tooltip)}
</DeckGL>
);
}

我使用了一些第三方包来使工具提示在 map 中显示。

renderTooltip props 由输入 renderTooltip 分配我上面提到的函数,我使用 tooltip状态来控制是否应该在页面中显示工具提示。

PS:根据deck.gl的文档,onHover将在以下时间触发:

onHover (Function, optional)

This callback will be called when the mouse enters/leaves an object of this deck.gl layer with the following parameters:

  • info

  • event - the source event

根据错误跟踪路径,它在我的 <span> 中抛出错误元素,这就是为什么我认为它是 renderTooltip首先是问题。

Warning: React does not recognize the `viewState` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `viewstate` instead. If you accidentally passed it from a parent component, remove it from the DOM element.
in span (created by Map)
in div (created by DeckGL)
in div (created by DeckGL)
in DeckGL (created by Map)
in Map (created by SpatialTemporal)
in section (created by SpatialTemporal)
in SpatioMap
in section
in Unknown (created by LBS)
in div
in div
in Unknown (created by LBS)
in LBS (created by Connect(LBS))
in Connect(LBS) (created by DynamicComponent)
in DynamicComponent (created by Route)
in Route (created by BasicLayout)
in Switch (created by BasicLayout)
in div (created by BasicLayout)
in div (created by Basic)
in Basic (created by Adapter)
in Adapter (created by BasicLayout)
in div (created by BasicLayout)
in BasicLayout (created by Adapter)
in Adapter (created by BasicLayout)
in div (created by BasicLayout)
in BasicLayout (created by Adapter)
in Adapter (created by BasicLayout)
in div (created by ContainerQuery)
in ContainerQuery (created by BasicLayout)
in DocumentTitle (created by SideEffect(DocumentTitle))
in SideEffect(DocumentTitle) (created by BasicLayout)
in BasicLayout (created by Connect(BasicLayout))
in Connect(BasicLayout) (created by DynamicComponent)
in DynamicComponent (created by Route)
in Route (created by DvaRoot)
in Switch (created by DvaRoot)
in Router (created by DvaRoot)
in LocaleProvider (created by DvaRoot)
in Provider (created by DvaRoot)
in DvaRoot

最佳答案

我今天遇到了同样的问题,并通过不将工具提示添加为 DeckGL 的直接子项来解决它:

return (
<React.Fragment>
<DeckGL
width="100%"
height="100%"
layers={renderLayers()} // eslint-disable-line
initialViewState={initialViewState}
controller={controller}
>
{baseMap && (
<StaticMap
onLoad={addControlHandler}
reuseMaps
mapStyle="mapbox://styles/mapbox/dark-v9"
preventStyleDiffing
mapboxApiAccessToken={MAPBOX_TOKEN}
/>
)}
</DeckGL>
{object && renderTooltip(tooltip)}
</React.Fragment>
);

关于javascript - 警告 : React does not recognize the `viewState` prop on a DOM element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54300172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com