如何解决react-spring:将儿童映射到动画,导致样式消失
我正在使用react
和react-spring
来尝试构建具有幻灯片显示效果的东西,但是我认为我的React原理全都倒过来了。我的问题是我想停止接收特定的警告。我将发布我认为是精简且可复制的版本。
我在这里有三个目标:
// marquee.spring.tsx
import { animated,useTransition } from "react-spring"
import { Children,useCallback,useState } from "react"
import { defaultview,defaultConfig } from "./defaults"
type SpringMarqueeProps = {...}
// In my case,members of `children` are all <div />s,for clarity
const SpringMarquee = ({
children,config = defaultConfig,view: View = defaultview
}: SpringMarqueeProps) => {
// Store child count
const childCount = Children.count(children)
// Extract config
const { duration } = config
// Generate animated children
const animatedChildren = Children.map(children,(child: JSX.Element) => {
// Case 1: React complains about children being objects,page does not render
const Child = animated(child)
// Case 2: React complains about an unmounted component update,page does render
const Child = animated(() => child) // this makes sense as `child` is <div />
// I want to pass the `styles` generated to the children but there is no evidence
// that this works at all for me
return props => <Child {...props} />
})
// Pagination
const [index,setIndex] = useState(0)
// Paginator
const next = useCallback(() => setIndex(i => ++i % childCount),[])
// Generating styles
const transition = useTransition(index,i => i,{/*...styles*/})
// Interval
useEffect(() => {
const int = setInterval(next,duration)
return () => clearInterval(int)
},[])
// Tidying up
// It is here that I am struggling to apply the generated styles to
// the `animatedChildren`
const Result = props => (
// In an odd twist,props are passed to `View` just fine
<View {...props}>
{
transitions.map(({ item,props: style,key }) => {
// Case 3: Despite me passing `style`,nothing appears on the
// children in the DOM when they render
const MarqueeItem = animatedChildren[item]
return <MarqueeItem key={key} style={style} />
// Case 4: I try to ensure props are passed to the children
// by forcing them to be JSX,but still no `style` attribute
const Child = animatedChildren[item]
const MarqueeItem = props => <Child {...props} />
return <MarqueeItem key={key} style={style} />
})
}
</View>
)
return <Result />
}
export default SpringMarquee
上面的组件被这样使用:
// index.tsx
import SpringMarquee from "components/dynamic/spring/marquee/marquee.spring"
export const IndexPage = () => {
return (
<SpringMarquee>
{/* default props are handled silently */}
<div>Hello</div>
<div>Hi</div>
<div>How are you?</div>
</SpringMarquee>
)
}
export default IndexPage
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。