Chuyển đến nội dung chính

Bài 12: ReactJS - tìm hiểu về Animations trong React


Trong bài viết này chúng ta sẽ tìm hiểu về animation css trong React.

Step-1: Cài đặt React CSS Transitions Group


Đây là một React add-on phổ biến cho việc tạo CSS transitions và animations. Chúng ta sẽ cài đặt nó từ command prompt của window.



C:\Users\username\Desktop\reactApp>npm install react-addons-css-transition-group

Step-2: Tạo CSS file


Hãy tạo 1 thư mục css trong thư mục public và tạo 1file style.css trong thư mục đó. Đừng quên link nó vào head trong index.html

<link rel = "stylesheet" type = "text/css" href = "css/style.css">

Step-3: Appear animation.

Chúng ta sẽ tạo 1 React component cơ bản. Thẻ ReactCSSTransitionGroup được sử dụng để bao cái thằng component mà ta muốn nó animate.

App.js


import React from 'react';
var ReactCSSTransitionGroup = require('react-addons-css-transition-group');

class App extends React.Component {
   render() {
      return (
         <div>
            <ReactCSSTransitionGroup transitionName = "example"
               transitionAppear = {true} transitionAppearTimeout = {500}
               >
     
               <h1>My Element...</h1>
            </ReactCSSTransitionGroup>
         </div>      
      );
   }
}
export default App;
transitionAppear = {true} khởi tạo transition ngay khi component start Mount ( các bạn xem bài này để hiểu về vòng đời của component trong React: Bài 7: ReactJS - Tìm hiểu về Component Life Cycle, transitionAppear sẽ khởi tạo lúc componentWillMount).
transitionAppearTimeout = {500} thời gian xuất hiện giống trong css transition bình thường.

Index.js


import React from 'react'
import ReactDOM from 'react-dom';
import App from './App.jsx';

ReactDOM.render(<App />, document.getElementById('app'));

css/style.css
.example-appear {
   opacity: 0.01;
}
.example-appear.example-appear-active {
   opacity: 1;
   transition: opacity 500ms ease-in;
}
Khi chạy project ta sẽ thấy component từ từ xuất hiện Fade in.

Step 4 - Enter and Leave Animations

Chúng ta sẽ sử dụng Enter and Leave Animations để add và remove phần tử trong list component.

App.js

import React from 'react';
var ReactCSSTransitionGroup = require('react-addons-css-transition-group');

class App extends React.Component {
   constructor(props) {
      super(props);
  
      this.state = {
         items: ['Item 1...', 'Item 2...', 'Item 3...', 'Item 4...']
      }
      this.handleAdd = this.handleAdd.bind(this);
   };
   handleAdd() {
      var newItems = this.state.items.concat([prompt('Create New Item')]);
      this.setState({items: newItems});
   }
   handleRemove(i) {
      var newItems = this.state.items.slice();
      newItems.splice(i, 1);
      this.setState({items: newItems});
   }
   render() {
      var items = this.state.items.map(function(item, i) {
         return (
            <div key = {item} onClick = {this.handleRemove.bind(this, i)}>
               {item}
            </div>
         );
      }.bind(this));
      
      return (
         <div>      
            <button onClick = {this.handleAdd}>Add Item</button>
    
            <ReactCSSTransitionGroup transitionName = "example" 
               transitionEnterTimeout = {500} transitionLeaveTimeout = {500}>
               {items}
            </ReactCSSTransitionGroup>
         </div>
      );
   }
}
export default App;
Tìm hiểu function splice() ở đây.
Index.js
import React from 'react'
import ReactDOM from 'react-dom';
import App from './App.jsx';

ReactDOM.render(<App />, document.getElementById('app'));
CSS/style.css
.example-enter {
   opacity: 0.01;
}
.example-enter.example-enter-active {
   opacity: 1;
   transition: opacity 500ms ease-in;
}
.example-leave {
   opacity: 1;
}
.example-leave.example-leave-active {
   opacity: 0.01;
   transition: opacity 500ms ease-in;
}
Khi chúng ta chạy chương trình và nhấn button Add Item kết quả ta được:
Khi ta click ok hoặc nhấn enter 1 phần tử mới sẽ xuất hiện từ từ

Khi ta xóa 1 item khi click vào nó, như Item 3 trong ví dụ, phần tử sẽ từ từ biến mất.

Nhận xét

  1. Mong những bài viết tiếp từ anh, hi vọng sau khi xong những bài hướng dẫn cơ bản sẽ có những hướng dẫn bài tập thực hành, hoặc áp dụng react js vào xây dựng những ứng dụng thực tế. Cảm ơn anh!

    Trả lờiXóa
    Trả lời
    1. Cảm ơn comment của bạn, comment của bạn góp phần tạo nên động lực để mình tiếp tục xây dựng blog.

      Xóa
  2. Những bài viết của anh rất dễ hiểu và dễ học.cảm ơn anh rất nhiều!

    Trả lờiXóa

Đăng nhận xét

Bài đăng phổ biến từ blog này

Tổng hợp danh sách các trang web lấy backlink cực tốt (P.1)

Đi backlink  trong SEO là điều mà ai cũng phải biết và đầu tư cho nó nhiều nhất. Dưới đây, mình chia sẻ các trang web có thể đi backlink khá tốt cho mọi người. Mỗi ngày các bạn tạo 1 bài post sau đó post lên 10 trang trong tổng số 40 trang này, hôm sau cũng viết 1 bài khác rồi post lên 10 trang kế, khi hết thì quay vòng lại 10 trang đầu, mình sẽ update thêm các website nên các bạn cứ yên tâm không lo hết. (Các bạn nhớ bookmark lại kẻo quên trang mình nha :) ) Nếu các bạn không rõ về DA PA IN-EXTERNAL Links thì xem ở đây:  Các chỉ số quan trọng cần biết khi làm SEO ID URL DA PA Internal Links External Links Alexa Global Rank Alexa Local Rank 1 http://diendan.zing.vn/ 63,17 54,5 168 19 701 9 2 http://vatgia.com/ 62,61 57,34 984 23 8086 71 3 http://forum.ueh.edu.vn/ 49,82 38,88 555 43 92060 774 4 http://www.5giay.vn/ 47,12 55,72 445 ...

5 minute setup Firebase for .NET C#

  Step 1: - Access firebase console and create a project: - At the project you just created, go to Firestore Database and create a collection you want: - Next, go to the project setting: - At tab  Service account generate your private key. Step 2: - Create a C# project. - Use Nuget to install following packages: - Create folder to store private key. - Finally, the code to make everything run: using Google.Apis.Auth.OAuth2; using Google.Cloud.Firestore; using Google.Cloud.Firestore.V1; using Google.Cloud.Storage.V1; using Grpc.Auth; using Grpc.Core; using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; namespace FireBaseConnect {     class Program     {         static void Main(string[] args)         {             MyFireStore myfs = new MyFireStore();             myfs.GetLicense().Wait();         }...

7 kĩ thuật tối ưu seo blogspot cơ bản cần biết

Là một new blogger ắt hẳn mọi người còn lạ lẫm với blogspot chưa có nhiều thời gian để nghiên cứu, tìm hiểu về nó, do đó hôm nay mình xin chia sẻ với các bạn các tips cơ bản nhưng rất cần thiết giúp t ối ưu hóa seo cho blogspot . Chúng ta bắt đầu nào. 1. Thay đổi đường dẫn URL của bài viết trước khi publish. Thông thường khi bạn tạo 1 bài post, đường dẫn URL sẽ được sinh tự động dựa theo tiêu đề, nhưng nếu tiêu đề dài nó sẽ tự động rút ngắn lại, điều này làm cho đường dẫn không đầy đủ đôi khi còn bị sai từ khóa, do đó chúng ta nên tự đặt tên cho đường dẫn của mình để tối ưu SEO . Để sửa đường dẫn các bạn làm theo hình Vào phần cài đặt bài đăng, mục liên kết cố định chọn liên kết cố định tùy chỉnh và sửa phần text cho phù hợp với nội dung bài viết rồi click hoàn thành. 2. Kiểm soát mật độ từ khóa trong bài viết. Ai cũng biết có nhiều từ khóa cần seo trong bài viết sẽ tốt nhưng nhiều quá thì sẽ gây ngộ độc và có thể bị gắn mác spam. Mật độ từ khóa thích hợp là 2%/po...