React Spring默认对象

React Spring默认对象

在本文中,我们将学习如何使用React Spring的默认对象。 React Spring 是一个使UI元素动画化变得简单的动画库。它基于弹簧物理学,帮助实现自然的外观和感觉。它与其他动画库不同,其他动画库需要处理曲线、缓动和时间持续等,而React Spring中这些都是同步进行的。

React Spring是一个跨平台库,它支持React、React Native、web和许多其他平台。它也支持所有浏览器。

默认对象: 用于设置props的默认值。你也可以传递一个对象,而不是使用default: true。

创建React应用的步骤:

步骤1: 使用以下命令创建一个新的应用程序。

npx create-react-app reactspringdemo

步骤2: 现在使用以下命令移动已创建的项目文件夹。

cd reactspringdemo

步骤3: 安装React Spring库。

npm install react-spring

项目结构:

React Spring默认对象

运行应用程序的步骤:

在终端中输入以下命令来运行应用程序:

npm start

示例1: 在下面的代码中,我们将使用默认对象来设置默认属性。

GFG.jsx

import React from 'react'; 
import { useSpring, animated } from 'react-spring'
  
const CompatibleProps = () => { 
  
    /** 
    * Define the style for the animation 
    * using the useSpring hook 
    */
    const styles = useSpring({ 
        loop: true, 
        default: { immediate: true }, 
        from: { rotateZ: 0 }, 
        to: { rotateZ: 360 }, 
        duration: 2000, 
    }); 
  
    /** 
    * Animated div is the extended version of div that 
    * accepts the properties defined above. 
    */
    return (<animated.div 
        style={{ 
            width: 80, 
            height: 80, 
            backgroundColor: 'd6d6d6', 
            borderRadius: 16, 
            boxShadow: 'rgb(0,0,0,0.44) 0px 5px 5px', 
            display: 'flex', 
            alignItems: 'center', 
            justifyContent: 'center', 
            color: 'green', 
            margin: 250, 
            ...styles, 
        }} >GFG</animated.div> 
    ); 
} 
  
export default CompatibleProps; 

App.js

import React from 'react'
import GFG from './GFG'
  
function App() { 
    console.log('hello') 
    return ( 
        <> 
            <GFG /> 
        </> 
    ); 
} 
  
export default App;

输出:

React Spring默认对象

示例2:

在下面的代码中,我们将利用默认对象来设置默认属性。

GFG.jsx

import React from 'react'; 
import { useSpring, animated } from 'react-spring'
  
const CompatibleProps = () => { 
  
    /** 
    * Define the style for the animation 
    * using the useSpring hook 
    */
    const styles = useSpring({ 
        loop: true, 
        default: { immediate: false }, 
        from: { rotateZ: 0 }, 
        to: { rotateZ: 360 }, 
        duration: 2000, 
    }); 
  
    /** 
    * Animated div is the extended version of div that 
    * accepts the properties defined above. 
    */
    return (<animated.div 
        style={{ 
            width: 80, 
            height: 80, 
            backgroundColor: 'd6d6d6', 
            borderRadius: 16, 
            boxShadow: 'rgb(0,0,0,0.44) 0px 5px 5px', 
            display: 'flex', 
            alignItems: 'center', 
            justifyContent: 'center', 
            color: 'green', 
            margin: 250, 
            ...styles, 
        }} >GFG</animated.div> 
    ); 
} 
  
export default CompatibleProps;

App.js

import React from 'react'
import GFG from './GFG'
  
function App() { 
    console.log('hello') 
    return ( 
        <> 
            <GFG /> 
        </> 
    ); 
} 
  
export default App;

输出:

React Spring默认对象

参考链接: https://react-spring.dev/common/props#imperative-updates

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程