【问题标题】:Svg path is not animating in React Spring animating librarySvg 路径在 React Spring 动画库中没有动画
【发布时间】:2019-04-13 22:36:32
【问题描述】:
【问题讨论】:
标签:
reactjs
svg
react-spring
【解决方案1】:
import { useEffect, useRef, useState } from 'react'
import { useSpring } from '@react-spring/core'
import { animated } from '@react-spring/web'
export const CirclePath = () => {
const circleRef = useRef()
const [offset, setOffset] = useState(null)
useEffect(() => setOffset(circleRef.current.getTotalLength()), [offset])
const config = useSpring({
from: { offset },
to: { offset: 0 },
})
return (
<svg>
<animated.circle
ref={circleRef}
cx='50'
cy='50'
r='30'
strokeDashoffset={config.offset}
strokeDasharray={offset}
stroke='black'
strokeWidth='2'
fill='none'
></animated.circle>
</svg>
)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>