


New writing method of react official website animation library (react-transition-group)
This article mainly introduces the new way of writing the react official website animation library (react-transition-group), which has a certain reference value. Now I share it with you. Friends in need can refer to it
一, react-transition-group is an animation transition library provided by the official website.
I searched the Internet about react animation. Many of the answers are old versions from a long time ago, and now the official one is called react-transition-group
It is the previous two The combined version, so I wrote this down to record it, and also gave some tips to those who need it.
1. Install
# npm npm install react-transition-group --save # yarn yarn add react-transition-group
and the three officially provided components Transition
, CSSTransition
, TransitonGroup
.
Transition
Transition components (Transiton) allow you to describe the transition from one component state to another over time with a simple declarative API. Most commonly used to animate the installation and uninstallation of a component, but can also be used to describe transition states where appropriate. By default, this component does not change the behavior of the component it renders, it only tracks the "entering" and "exiting" states of the component. It's up to you to define the effects for these states. (Translation)
The actual situation is that if you only write this component, it will have no effect, just like if you write a p. So you need to give them some parameters. For example, the following example
//自己简单的封装了一个,该有的参数都由了,可以自行粘贴在自己的代码里面去试试。 class Fade extends React.Component { constructor(props) { super(props); } done =() => { // console.log('结束了') } addaddEndListener = (node) => { //原生时间transition运动的事件 node.addEventListener('transitionend', this.done, false); } // 三个进入状态的事件,可以做你想做的事情 onEnter = (node, isAppearing) => { console.log(isAppearing, 'onEnter') } onEntering = (node, isAppearing) => { console.log(isAppearing, 'onEntering') } onEntered = (node, isAppearing) => { console.log(isAppearing, 'onEntered') } // 三个退出的状态的事件 onExit = (node) => { console.log('onExit') } onExiting = () => { console.log('onExiting') } onExited = () => { console.log('onExited') this.props.self() } render() { const { in: inProp, dur} = this.props; const defaultStyle = { transition: `transform ${300}ms ease-in-out, opacity ${300}ms ease-in-out`, transform: 'translateX(100px)', opacity: '0' } const transitionStyles = { entering: { transform: 'translateX(100px)', opacity: '0'}, entered: { transform: 'translateX(0px)', opacity: '1' }, exiting: {transform: 'translateX(0px)', opacity: '1'}, exited: {transform: 'translateX(0px)', opacity: '0'} }; const duration = { enter: 300, exit: 300, } // 上面的都是基本参数,样式的转变以及时间的设定,具体的可以看官网文档 // 样式也可以写成className 例如<mycomponent></mycomponent> return ( <transition> { state => { console.log(state, '###') //你可以很直观的看到组件加载和卸载时候的状态 return( <p> {this.props.children} </p> ) } } </transition> ); } }
There are four states above:
entering entered exiting exited
The initialization state of the component stays at exited
The above is the most basic one written Example, the following is how to call
let num = 1; class Dnd extends React.Component { state = { ins: true, current: 1, dom: <p> ceshi weizhi {num} </p> } handle = (bool) => { this.setState({ test: !bool }) } end = () => { const that = this; num = num + 1; setTimeout(function () { that.setState({ test: true, dom: <p> 888888 {num} </p> }) }, 500) } render () { const { location } = this.props const { test } = this.state; return ( <mainlayout> <button>点击transition</button> <fade> {this.state.dom} </fade> </mainlayout> ) } } // 效果是每次点击按钮都会进行一次进场和出场的动画。也可以自行衍生。
这个组件大概就是这样的,这样适合写一个tab类型的页面,在切换的时候可以展示不同的dom
CSSTransition
This component applies a pair of class names (that is, className) in the emergence, entry, and exit phases of the transition, and relies on this to activate CSS animation. Therefore, the parameters are different from the usual className. The parameters are: classNames
Parameters: (mainly) in, timeout, unmountOnExit, classNames, the usage is the same as Transition. Look at the following example:
state = { star: false } <button>start</button> <csstransition> <p>⭐</p> </csstransition>
The effect is to click the button to display the stars, and click the animation of the stars to disappear!
ParametersclassNames="star"
, the component will find the className of the corresponding state, as follows
.star { display: inline-block; margin-left: 0.5rem; transform: scale(1.25); } .star-enter { opacity: 0.01; transform: translateY(-100%) scale(0.75); } .star-enter-active { opacity: 1; transform: translateY(0%) scale(1.25); transition: all 300ms ease-out; } .star-exit { opacity: 1; transform: scale(1.25); } .star-exit-active { opacity: 0; transform: scale(4); transition: all 300ms ease-in; }
The order of execution is
1、星星显示 对应的class为star-enter star-enter-active 动画走完为star-enter-done 2、星星消失 对应的class为star-exit star-exit-active 动画走完为star-exit-done
If you follow the explanation on the official website, it is As follows, if you are interested, you can try it yourself.
classNames={{ appear: 'my-appear', appearActive: 'my-active-appear', enter: 'my-enter', enterActive: 'my-active-enter', enterDone: 'my-done-enter, exit: 'my-exit', exitActive: 'my-active-exit', exitDone: 'my-done-exit, }}
The hook function of each stage is the same as Transition.
TransitionGroup
As soon as you look at the group, you will know that it must be a list-shaped animation! But after reading the official website, I learned that TransitionGroup does not provide any form of animation. The specific animation depends on the animation of the Transition || CSSTransition you wrapped, so you can make different types of animations in the list. Let’s take a look at an example
class List extends React.Component { constructor(props) { super(props); this.state = { items: [ { id: 1, text: 'Buy eggs' }, { id: 2, text: 'Pay bills' }, { id: 3, text: 'Invite friends over' }, { id: 4, text: 'Fix the TV' }, ] } } render() { const { items } = this.state; return ( <p> <transitiongroup> {items.map(({ id, text }) => ( <csstransition> <p> <button> { this.setState(state => ({ items: state.items.filter( item => item.id !== id ), })); }} > × </button> {text} </p> </csstransition> ))} </transitiongroup> <button> { const text = prompt('Enter some text'); if (text) { this.setState(state => ({ items: [ ...state.items, { id: 1123, text }, ], })); } }} > Add Item </button> </p> ); } }
css
.fade-enter { opacity: 0.01; } .fade-enter-active { opacity: 1; transition: opacity 500ms ease-in; } .fade-exit { opacity: 1; } .fade-exit-active { opacity: 0.01; transition: opacity 500ms ease-in; }
The effect is to add and delete one of the list items, resulting in a fading effect! To put it bluntly, it is the effect of a combination of multiple Transitions or CSSTransitions.
But it also provides some parameters
1、component default 'p' 也就是TransitionGroup渲染出来的标签为p,也可以就行更改,例如component="span" 渲染出来的就是span标签了 2、children 相当于你给的组件,可以是字符串或者自定义组件等。 3、appear 写在TransitionGroup里面相当于开启或者禁止里面的Transition || CSSTransition 方便写的作用
The approximate characteristics of the three components are these. The rest is all up to you to develop! Some small examples will be included in the follow-up to explain, so stay tuned. . . .
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Native JS encapsulates the vertical scrolling animation tool function based on window.scrollTo()
Jquery adds loading Transition Mask
The above is the detailed content of New writing method of react official website animation library (react-transition-group). For more information, please follow other related articles on the PHP Chinese website!

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
