reactjs - 修改状态变量的属性
问题描述
我在我的 React 应用程序中使用heatmap.js 。我需要对浏览器调整大小和重新计算数据做出反应。为了检测浏览器调整大小,我使用react-use-measure
包。
工作代码:
import useMeasure from 'react-use-measure';
import HeatMapJs from 'heatmap-ts';
export const Heatmap: FC<HeatmapProps> = ({
backgroundImageUrl,
data,
opacity = 0.5,
radius = 20,
}: HeatmapProps) => {
const [ref, bounds] = useMeasure({ polyfill: ResizeObserver });
const [hm, setHm] = useState<HeatMapJs>();
useEffect(() => {
setHm(
new HeatMapJs({
container: document.getElementById('hm') || undefined,
opacity,
radius,
})
);
}, []);
useEffect(() => {
if (data.length && bounds.width) {
const ratioX = bounds.width / 1280; // hardcoded width of full image
const ratioY = bounds.height / 720;
let min: number = data[0].value;
let max: number = data[0].value;
const len = data.length;
const dataWithRatio: HeatmapPoint[] = [];
for (let i = 0; i < len; i++) {
const item = data[i];
const v = item.value;
min = min < v ? min : v;
max = max > v ? max : v;
dataWithRatio.push({
x: Math.round(item.x * ratioX),
y: Math.round(item.y * ratioY),
value: item.value,
} as HeatmapPoint);
}
if (min === max) {
min = 0;
}
hm?.setData({
min: min,
max: max,
data: dataWithRatio,
});
}
}, [bounds]);
return (
<div>
<div id="hm" ref={ref}>
<img src={backgroundImageUrl} />
</div>
</div>
);
};
我使用状态变量hm
- 它必须创建一次,因为每次创建时都会将画布添加到div#hm
. 然后,当bounds
更改生效时,我计算新数据添加替换现有的hm
.
这可行,但看起来不遵循 React 状态的不变性规则,因为我更改了hm
.
我尝试将常规变量用于hm
:声明 const 而不是 state,在第一个效果中实例化它并在第二个效果中使用,但如果发生的话undefined
。
这应该如何以最正确的方式完成?我正在考虑手动删除旧画布,但它看起来像相当复杂的冗余。
解决方案
推荐阅读
- python - 通过“工作流程”将数据发布到数据库(例如:字段更改为 20,创建新记录)
- laravel - 如何修复“Illuminate\Support\Collection::get(), 0passed in /AMPPS/www/lsapp/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php”
- python - 为什么下面的代码中需要“lambda”
- php - 模态中的 jQuery 验证通过 php 发送数据
- powerbi - 如何将计算列的每一行除以另一个计算列的总数?
- json - 如何选择 * WHERE JSON 键包含 ANY/IN(值数组)
- c# - 多行 JSON 到数据表
- google-cloud-platform - 有没有办法在模拟服务帐户时使用 gsutil?
- websocket - Squid4 转发代理从 ws 升级到 wss
- javascript - 防止数组在文本输入上循环并从该数组中提取值 | Vue