React Spring 取消属性
在本文中,我们将学习取消属性的工作原理。
React spring 是一个使UI元素动画变得简单的动画库。它基于弹簧物理学,可以实现自然的外观和感觉。它与其他动画库不同,其他库中需要处理曲线、缓动以及时间持续等,并且它们都是同步的。
平台: React spring是一个跨平台库,它支持react、react-native、web和许多其他平台。它还支持所有浏览器。
取消属性: 当取消设置为true时,循环动画将停止工作,如果取消设置为false,循环将重新开始。
语法:
useSpring({ 
    cancel: // value
})
创建 React Spring 应用的步骤:
步骤1: 使用以下命令创建一个新的应用程序。
npx create-react-app reactspringdemo
步骤2: 现在使用以下命令移动创建的项目文件夹。
cd reactspringdemo
步骤3: 安装React Spring库。
npm install react-spring
项目结构:

按照以下步骤运行项目:
npm start 
示例1: 在下面的代码中,我们将使用上述语法来演示cancel prop的使用。
index.html
<!DOCTYPE html> 
<html lang="en"> 
  
<title>React App</title> 
</head> 
  
<body> 
      
    <center> 
        <h1 style="color: green;">GeeksforGeeks</h1> 
        <h3>A computer science portal for geeks</h3> 
        <h2>React Spring Cancel Prop</h2> 
        <h3>cancel: false</h3> 
  
        <div id="root"></div> 
    </center> 
</body> 
  
</html>
GFG.jsx
import React from 'react'; 
import { useSpring, animated } from 'react-spring'
  
function LoopTrue() { 
    const styles = useSpring({ 
        loop: true, 
        from: { rotateZ: 0 }, 
        to: { rotateZ: 180 }, 
        cancel: false
    }) 
  
    return ( 
        <animated.div 
            style={{ 
                width: 80, 
                height: 80, 
                backgroundColor: 'green', 
                borderRadius: 25, 
                ...styles, 
            }} 
        /> 
    ) 
} 
export default LoopTrue; 
App.js
import React from 'react'
import GFG from './GFG'
  
function App() { 
    console.log('hello') 
    return ( 
        <> 
            <GFG /> 
        </> 
    ); 
} 
  
export default App; 
输出:

示例2:
在下面的代码中,我们将使用上述语法来演示取消prop的使用。在这个示例中,我们通过将cancel属性更改为true来停止动画。
index.html
<!DOCTYPE html> 
<html lang="en"> 
  
<title>React App</title> 
  
<body> 
  
    <center> 
        <h1 style="color: green;">GeeksforGeeks</h1> 
        <h3>A computer science portal for geeks</h3> 
        <h2>React Spring Cancel Prop</h2> 
        <h3>cancel: true</h3> 
  
        <div id="root"></div> 
    </center> 
</body> 
  
</html>
GFG.jsx
import React from "react"; 
import { useSpring, animated } from "react-spring"; 
  
function LoopTrue() { 
    const styles = useSpring({ 
        loop: true, 
        from: { rotateZ: 0 }, 
        to: { rotateZ: 180 }, 
        cancel: true, 
    }); 
  
    return ( 
        <animated.div 
            style={{ 
                width: 80, 
                height: 80, 
                backgroundColor: "green", 
                borderRadius: 25, 
                ...styles, 
            }} 
        /> 
    ); 
} 
export default LoopTrue; 
App.js
import React from 'react'
import GFG from './GFG'
  
function App() { 
    console.log('hello') 
    return ( 
        <> 
            <GFG /> 
        </> 
    ); 
} 
  
export default App; 
输出:

参考: https://react-spring.dev/common/props#cancel-prop
极客教程