🔏
PexpA
  • Home
  • Knowledge
    • Design Pattern
    • RxJS
    • Computer Graphics
      • Phép biến đổi hình học
    • Javascript
      • Generator function và Yield trong javascript
      • Asynchronous và Synchronous
    • GraphQL
      • Core Concepts
      • Xây dựng GraphQL sử dụng NodeJS
    • Analysis and System Design
    • SEO
    • Database
      • NoSQL
        • MongoDB
      • SQL
    • ReactJS
      • React Fragment
      • Lifecycle trong component
      • HOCs
      • What is Ref ?
      • Context API
      • React Hooks
        • useState Hook
        • useEffect Hook
        • useLayoutEffect Hook
        • Khi nào dùng useLayoutEffect và useEffect
        • useContext Hook
        • useReducer Hook
        • useCallback Hook
        • useMemo Hook
        • useRef Hook
        • Building Your Own Hooks
      • Redux
    • React Native
      • Animations
    • Angular
    • Python
      • Object Oriented Programming
      • Decorator
      • Multi Threading
      • Generators
      • Iterators
    • Java
    • Blockchain
      • Ethereum Development Overview
      • Solidity Document
      • JSON RPC Protocol
  • Package
    • React Router V4
    • API Documentation
      • API Blueprint
      • Swagger
    • Lazyload image
    • React Helmet
    • React Spring
    • React Apollo
    • ImmerJS
    • Styled components
  • Experience
    • Sử dụng Latex trên VSCode
    • Linked List in C++
    • How to using Date, Time, TimeStamp for Java to connect Database
    • Pass props to a component rendered by React Router v4
    • Forking Workflow
  • Deploy
    • Heroku
      • How to deploy React App with Express
      • How to deploy React App with Python
      • How to deploy React App with Java
  • About me
Powered by GitBook
On this page
  • Side effects là gì?
  • Khái nhiệm
  • Phân loại Side effects
  • Giới thiệu React.useEffect Hook
  • Vòng đời hoạt động của React.useEffect
  • Các trường hợp sử dụng dependencies

Was this helpful?

  1. Knowledge
  2. ReactJS
  3. React Hooks

useEffect Hook

Side effects là gì?

Khái nhiệm

Việc fetching data, thiết lập các subscription, và việc thay đổi DOM trong React component, những hành động như vậy được gọi là side effect(hoặc effect).

Phân loại Side effects

  • Effects không cần clean up: gọi API, tương tác DOM

  • Effects cần clean up: subscription, setTimeout, setInterval

Giới thiệu React.useEffect Hook

React.useEffect là một hook cơ bản và có sẵn trong React hooks, nó được sử dụng cho Side effects và thực thi ít nhất 1 lần sau lần render đầu tiên, những lần render sau chỉ được thực thi nếu có dependencies thay đổi. React.useEffect gồm 2 phần: side effect và clean up (optional) và effect clean up sẽ được thực thi trước run effect lần tiếp theo hoặc unmount.

// callback: Your side effect function
// dependencies: Only excute callback if one of your dependencies changes
function useEffect(callback, dependencies) {}

Một đoạn code mẫu mô phỏng useEffect

function App() {
    // executed before each render
    const [color, setColor] = useState('deeppink');
    // executed after each render
    useEffect(() => {
    // do your side effect here ...
    return () => {
    // Clean up here ...
    // Executed before the next render or unmount
    };
    }, []);
    // rendering
    return(
        //TODO:code
    );
}

Vòng đời hoạt động của React.useEffect

Quá trình Mounting

Trong quá trình này component sẽ thực hiện rendering trước sau đó sẽ thực hiện hàm useEffect() và chỉ thực hiện side effect chứ không thực hiện cleanup trong lần render đầu tiên.

Quá trình Updating

Sau khi có cập nhật từ component lúc này sẽ thực hiện rendering và cleanup sẽ được kích hoạt, sau đó mới thực hiện side effect lần tiếp theo nếu dependencies thay đổi.

Quá trình Unmounting

Trong quá trình này sẽ chỉ chạy cleanup.

Các trường hợp sử dụng dependencies

Không khai báo dependencies

useEffect(() => {
    // EVERY
    // No dependencies defined
    // Always execute after every render
    return () => {
    // Execute before the next effect or unmount.
    };
});

Trong trường hợp này useEffect sẽ thực hiện sau mỗi lần render

Khai báo dependencies với một mảng trống

useEffect(() => {
    // ONCE
    // Empty dependencies
    // Only execute once after the FIRST RENDER
    return () => {
    // Execute once when unmount
    };
}, []);

Trong trường hợp này useEffect sẽ chỉ thực hiện 1 lần duy nhất sau lần render đầu tiên.

Khai báo dependencies với tham số

useEffect(() => {
    // On demand
    // Has dependencies
    // Only execute after the first RENDER or filters state changes
    return () => {
    // Execute before the next effect or unmount.
    };
}, [filters]);

Trong trường hợp này useEffect sẽ render lần đầu tiên nhưng sẽ thực hiện lần tiếp theo nếu tham số filters thay đổi.

PrevioususeState HookNextuseLayoutEffect Hook

Last updated 4 years ago

Was this helpful?