Làm cách nào để làm mới một trang mà không cần tải lại?

Trong bài viết này, chúng ta sẽ xem cách hiển thị các giá trị từ cơ sở dữ liệu mà không cần làm mới trang web. Giả sử trong ứng dụng web của chúng tôi, chúng tôi có một nút sau khi nhấp vào nút đó, một chức năng sẽ kích hoạt và tìm nạp dữ liệu từ cơ sở dữ liệu. Hoặc trong ứng dụng web React, chúng ta có thể sử dụng hook tự động lấy dữ liệu mỗi khi trang được mở, chẳng hạn chúng ta có thể sử dụng hook hiệu quả từ React

Tạo ứng dụng React.  

  • Bước 1. Tạo ứng dụng React bằng lệnh sau
npx create-react-app app-name 
  • Bước 2. Sau khi tạo thư mục dự án của bạn [tôi. e. my-app], di chuyển đến nó bằng cách sử dụng lệnh sau
cd app-name

Cấu trúc dự án. Nó sẽ trông giống thế này

Sử dụng hook useEffect. Cách tìm nạp dữ liệu khi một thành phần được gắn kết, cách chạy mã khi trạng thái thay đổi hoặc khi chỗ dựa thay đổi, cách thiết lập bộ hẹn giờ hoặc khoảng thời gian. useEffect chạy sau mỗi lần kết xuất [theo mặc định] và có thể tự dọn dẹp tùy chọn trước khi chạy lại. Vì vậy, sử dụng hook này, chúng tôi có thể hiển thị các giá trị từ cơ sở dữ liệu trong thời gian thực mà không cần tải lại trang web.  

cú pháp. Sau đây là cú pháp của useEffect hook

useEffect[[] => {
    // Your code
}, []] // Dependency-array

Lấy dữ liệu từ cơ sở dữ liệu thông qua API. Giả sử chúng ta có một API kết nối từ cơ sở dữ liệu, ở đây chúng ta đã sử dụng API ngẫu nhiên cung cấp dữ liệu giả. Sau khi nhấp vào dữ liệu chức năng handleFetchData sẽ tìm nạp và bạn có thể thấy dữ liệu đó tại bảng điều khiển của mình

const handleFetchData = async [e] => {
  // Put the endpoint of Api
  fetch['API_URL_ENDPOINT']
    .then[[resp] => resp.json[]]
    .then[[data] => console.log[data]]
    .catch[[e] => console.log[e]];
};

Thực hiện. Viết đoạn mã sau vào tệp tương ứng

Ứng dụng. js




cd app-name
5
cd app-name
6
cd app-name
7

cd app-name
8

cd app-name
9
cd app-name
0

cd app-name
1
cd app-name
2

cd app-name
8

cd app-name
1
cd app-name
5

cd app-name
6
cd app-name
7

cd app-name
6
cd app-name
9____20
useEffect[[] => {
    // Your code
}, []] // Dependency-array
1

useEffect[[] => {
    // Your code
}, []] // Dependency-array
2____23

useEffect[[] => {
    // Your code
}, []] // Dependency-array
2____25

useEffect[[] => {
    // Your code
}, []] // Dependency-array
6
useEffect[[] => {
    // Your code
}, []] // Dependency-array
7

useEffect[[] => {
    // Your code
}, []] // Dependency-array
2____29

cd app-name
1
const handleFetchData = async [e] => {
  // Put the endpoint of Api
  fetch['API_URL_ENDPOINT']
    .then[[resp] => resp.json[]]
    .then[[data] => console.log[data]]
    .catch[[e] => console.log[e]];
};
1
const handleFetchData = async [e] => {
  // Put the endpoint of Api
  fetch['API_URL_ENDPOINT']
    .then[[resp] => resp.json[]]
    .then[[data] => console.log[data]]
    .catch[[e] => console.log[e]];
};
2

cd app-name
8

cd app-name
1
const handleFetchData = async [e] => {
  // Put the endpoint of Api
  fetch['API_URL_ENDPOINT']
    .then[[resp] => resp.json[]]
    .then[[data] => console.log[data]]
    .catch[[e] => console.log[e]];
};
5

cd app-name
6
const handleFetchData = async [e] => {
  // Put the endpoint of Api
  fetch['API_URL_ENDPOINT']
    .then[[resp] => resp.json[]]
    .then[[data] => console.log[data]]
    .catch[[e] => console.log[e]];
};
7____38
const handleFetchData = async [e] => {
  // Put the endpoint of Api
  fetch['API_URL_ENDPOINT']
    .then[[resp] => resp.json[]]
    .then[[data] => console.log[data]]
    .catch[[e] => console.log[e]];
};
9

cd app-name
6
const handleFetchData = async [e] => {
  // Put the endpoint of Api
  fetch['API_URL_ENDPOINT']
    .then[[resp] => resp.json[]]
    .then[[data] => console.log[data]]
    .catch[[e] => console.log[e]];
};
31
const handleFetchData = async [e] => {
  // Put the endpoint of Api
  fetch['API_URL_ENDPOINT']
    .then[[resp] => resp.json[]]
    .then[[data] => console.log[data]]
    .catch[[e] => console.log[e]];
};
32
const handleFetchData = async [e] => {
  // Put the endpoint of Api
  fetch['API_URL_ENDPOINT']
    .then[[resp] => resp.json[]]
    .then[[data] => console.log[data]]
    .catch[[e] => console.log[e]];
};
9

cd app-name
6
const handleFetchData = async [e] => {
  // Put the endpoint of Api
  fetch['API_URL_ENDPOINT']
    .then[[resp] => resp.json[]]
    .then[[data] => console.log[data]]
    .catch[[e] => console.log[e]];
};
35____336
const handleFetchData = async [e] => {
  // Put the endpoint of Api
  fetch['API_URL_ENDPOINT']
    .then[[resp] => resp.json[]]
    .then[[data] => console.log[data]]
    .catch[[e] => console.log[e]];
};
9

cd app-name
6
const handleFetchData = async [e] => {
  // Put the endpoint of Api
  fetch['API_URL_ENDPOINT']
    .then[[resp] => resp.json[]]
    .then[[data] => console.log[data]]
    .catch[[e] => console.log[e]];
};
39____550
const handleFetchData = async [e] => {
  // Put the endpoint of Api
  fetch['API_URL_ENDPOINT']
    .then[[resp] => resp.json[]]
    .then[[data] => console.log[data]]
    .catch[[e] => console.log[e]];
};
9

cd app-name
1____553

cd app-name
8

cd app-name
1____556
cd app-name
57

cd app-name
6
cd app-name
59____560
cd app-name
61

useEffect[[] => {
    // Your code
}, []] // Dependency-array
2____563

useEffect[[] => {
    // Your code
}, []] // Dependency-array
2____565

cd app-name
8

useEffect[[] => {
    // Your code
}, []] // Dependency-array
2____568

useEffect[[] => {
    // Your code
}, []] // Dependency-array
6
cd app-name
56
cd app-name
57

_______572____573

_______574____575

_______572____577

useEffect[[] => {
    // Your code
}, []] // Dependency-array
6
cd app-name
79

useEffect[[] => {
    // Your code
}, []] // Dependency-array
2____581

cd app-name
6
cd app-name
83

cd app-name
1____579

cd app-name
86

cd app-name
8

cd app-name
88
cd app-name
89
cd app-name
90

Bước chạy ứng dụng. Chạy lệnh sau để khởi động ứng dụng

const handleFetchData = async [e] => {
  // Put the endpoint of Api
  fetch['API_URL_ENDPOINT']
    .then[[resp] => resp.json[]]
    .then[[data] => console.log[data]]
    .catch[[e] => console.log[e]];
};
3

đầu ra

Ghi chú. Móc useEffect sẽ tự động chạy mã để tìm nạp dữ liệu, do đó bạn không phải tải lại trang nhiều lần

Làm cách nào để làm mới trang mà không cần tải lại bằng javascript?

Làm mới trang khi nhấp vào nút . Trong ví dụ này, bạn có thể thấy trang đó sẽ được làm mới bằng cách sử dụng vị trí. reload[] sau khi người dùng nhấp vào nút Tải lại trang. call the method when a user performs an event, such as a button click. In this example, you can see that page will get refreshed using the location. reload[] method after the user clicks on the Reload Page button.

Tải lại một trang có giống như làm mới không?

AFAIK làm mới là khi trang [ví dụ:. mete refresh] hướng dẫn trình duyệt tải lại trang và tải lại [ví dụ:. nút trình duyệt] là khi người dùng quyết định yêu cầu trình duyệt làm mới trang .

Chủ Đề