微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

react-spring:将儿童映射到动画,导致样式消失

如何解决react-spring:将儿童映射到动画,导致样式消失

我正在使用reactreact-spring来尝试构建具有幻灯片显示效果的东西,但是我认为我的React原理全都倒过来了。我的问题是我想停止接收特定的警告。我将发布我认为是精简且可复制的版本。

在这里有三个目标:

  • 一个工作版本,没有“未安装React状态”错误
  • 一个语义上正确的版本可以正常工作(即我的可读和简单版本不会损坏)
  • 在渲染的子代上具有react-spring样式(到目前为止,我还无法做到)
// 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?