如何更新Motion.svg的大小?
如何更新Motion.svg的大小?
我有一个常规的Div(在商店中的“ MAKESELEMENT”),其中包含一个Motion.svg元素,该元素又包含SVG G容器中包裹的一些RECT元素。 我需要更新我的SVG元素才能查看所有RECT元素。我将状态设置在回调钩中,但是即使我看到自己的状态变化,我的SVG元素的高度仍然是100。 我正在使用MOBX-LITE的UseObserver挂钩
export interface SVGContainerProps {
store: myStore;
}
export default function SVGContainer(props: SVGContainerProps): ReactElement {
const {
store,
} = props;
const [height, setHeight] = useState<number>(100);
const useSVGRef = useCallback((svg: SVGSVGElement) => {
const bbox = svg.getBBox();
console.log('height:', bbox.height);
setHeight(bbox.height);
}, [])
return useObserver(() => (
<motion.svg
ref={useSVGRef}
className={cssSvgContainer}
dragConstraints={{ current: store.maskElement }}
dragMomentum={false}
drag={true}
whileTap={{ cursor: 'grabbing' }}
style={{ height: height}}
>
{store.items.map((item, index) => (
<SVGRect
key={item.id}
item={item}
x={0}
y={index * 10}
/>
))}
</motion.svg>
));
}
const cssSvgContainer = cx(
cssPointerEventsAll,
cssAbsolute,
cssBottom(0),
cssRight(0),
cssBorder,
cssBorderWidth1,
);
How to update the size of motion.svg?
I have a regular DIV ("maskElement" in my store) that contains a motion.svg element, that in turn contains some rect elements wrapped in SVG g Container.
I need to update my svg element to see all the rect elements. I set the state in a callback hook but the height of my svg element is still 100 even though I see my state change.
I'm using useObserver hook from mobx-lite
export interface SVGContainerProps {
store: myStore;
}
export default function SVGContainer(props: SVGContainerProps): ReactElement {
const {
store,
} = props;
const [height, setHeight] = useState<number>(100);
const useSVGRef = useCallback((svg: SVGSVGElement) => {
const bbox = svg.getBBox();
console.log('height:', bbox.height);
setHeight(bbox.height);
}, [])
return useObserver(() => (
<motion.svg
ref={useSVGRef}
className={cssSvgContainer}
dragConstraints={{ current: store.maskElement }}
dragMomentum={false}
drag={true}
whileTap={{ cursor: 'grabbing' }}
style={{ height: height}}
>
{store.items.map((item, index) => (
<SVGRect
key={item.id}
item={item}
x={0}
y={index * 10}
/>
))}
</motion.svg>
));
}
const cssSvgContainer = cx(
cssPointerEventsAll,
cssAbsolute,
cssBottom(0),
cssRight(0),
cssBorder,
cssBorderWidth1,
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论