1

我目前正在使用 reactn ( https://github.com/charlesStover/reactn ) 使用 react 和管理全局状态。

我必须使用“SVG”并且将来能够制作图表。我想要创建的每个矩形,并更新 x 和 y 坐标。

我的矩形存储在全局状态 => index.js

SetGlobal({rectangles : []})

我只需要使用当前矩形的索引设置数组矩形,而无需遍历整个数组的副本

const allRectangles = [...rectangles];

allRectangles[index].x = allRectangles[index].x - diffX;
allRectangles[index].y = allRectangles[index].y - diffY;
allRectangles[index].cercle1.x = allRectangles[index].cercle1.x - diffX;
allRectangles[index].cercle1.y = allRectangles[index].cercle1.y - diffY;
allRectangles[index].cercle2.x = allRectangles[index].cercle2.x - diffX;
allRectangles[index].cercle2.y = allRectangles[index].cercle2.y - diffY;

allRectangles[index].coords.x = e.pageX;
allRectangles[index].coords.y = e.pageY;

setRectangles(allRectangles);

我必须做这种练习,但要使用全局状态 https://codesandbox.io/s/0xo7y0wolv

我不知道我是否让自己理解,这对我来说还是新的。

4

0 回答 0