首页 > 解决方案 > 如何将 react 弹簧钩与 react 一起使用。基本代码不起作用

问题描述

动画只是不适用于此代码。我究竟做错了什么?

import React from 'react'
import { useSpring, animated } from 'react-spring'


function Comp1() {
  const props = useSpring({ opacity: 1, from: { opacity: 0 } })
  return (
      <animated.div style={c1Style}>

        <h1 style={{ color: "white" }}>Component 1</h1>
        <p>It is a long established fact that a reader will be distracted
        by the readable content of a page when looking at its layout.
        The point of using Lorem Ipsum is that it has a more-or-less
        normal distribution of letters, as opposed to using 'Content here,
         content here', making it look like readable English. </p>

      </animated.div>
  )
}

const c1Style = {
  background: 'steelblue',
  color: 'white',
  padding: '1.5rem'
}

export default Comp1

标签: reactjsreact-springreact-animated

解决方案


您还需要将弹簧动画添加props到 animation.div 样式:

function Comp1() {
  const props = useSpring({ opacity: 1, from: { opacity: 0 } })
  return (
    <animated.div style={{...c1Style, ...props }}>
      <h1 style={{ color: "white" }}>Component 1</h1>
      <p>It is a long established fact that a reader will be distracted
      by the readable content of a page when looking at its layout.
      The point of using Lorem Ipsum is that it has a more-or-less
      normal distribution of letters, as opposed to using 'Content here,
       content here', making it look like readable English. </p>
    </animated.div>
  )
}

弹簧动画


推荐阅读