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 Show Tạo ứng dụng React.
npx create-react-app app-name
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. jscd app-name5 cd app-name6 cd app-name7 cd app-name8 cd app-name9 cd app-name0 cd app-name1 cd app-name2 cd app-name8 cd app-name1 cd app-name5 cd app-name6 cd app-name7 cd app-name6 cd app-name9____20 useEffect(() => { // Your code }, []) // Dependency-array1 useEffect(() => { // Your code }, []) // Dependency-array2____23 useEffect(() => { // Your code }, []) // Dependency-array2____25 useEffect(() => { // Your code }, []) // Dependency-array6 useEffect(() => { // Your code }, []) // Dependency-array7 useEffect(() => { // Your code }, []) // Dependency-array2____29 cd app-name1 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-name8 cd app-name1 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-name6 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-name6 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-name6 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-name6 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-name1____553 cd app-name8 cd app-name1____556 cd app-name57 cd app-name6 cd app-name59____560 cd app-name61 useEffect(() => { // Your code }, []) // Dependency-array2____563 useEffect(() => { // Your code }, []) // Dependency-array2____565 cd app-name8 useEffect(() => { // Your code }, []) // Dependency-array2____568 useEffect(() => { // Your code }, []) // Dependency-array6 cd app-name56 cd app-name57 _______572____573 _______574____575 _______572____577 useEffect(() => { // Your code }, []) // Dependency-array6 cd app-name79 useEffect(() => { // Your code }, []) // Dependency-array2____581 cd app-name6 cd app-name83 cd app-name1____579 cd app-name86 cd app-name8 cd app-name88 cd app-name89 cd app-name90 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 . |