, untuk pengurusan cookie penyemak imbas yang diselaraskan. Kuki penyemak imbas menawarkan kaedah mudah untuk menyimpan data yang berterusan di seluruh sesi atau bahagian aplikasi. useCookie
1. Fungsi Utiliti Cookie
Sebelum membuat cangkuk, kami akan menentukan fungsi penolong untuk operasi kuki biasa: menetapkan, mengambil, dan mengeluarkan kuki.
setCookie
<code class="language-typescript">export function setCookie(key: string, value: unknown, expireDays = 1): void { const d = new Date(); d.setTime(d.getTime() + expireDays * 24 * 60 * 60 * 1000); document.cookie = `${key}=${value}; expires=${d.toUTCString()}; path=/`; }</code>
setCookie("theme", "dark", 7);
getCookie
<code class="language-typescript">export function getCookie(key: string): string | undefined { const name = `${key}=`; const decodedCookie = decodeURIComponent(document.cookie); const ca = decodedCookie.split(";"); for (let i = 0; i < ca.length; i++) { let c = ca[i]; while (c.charAt(0) === " ") { c = c.substring(1); } if (c.indexOf(name) === 0) { return c.substring(name.length, c.length); } } return undefined; }</code>
document.cookie
. undefined
const theme = getCookie("theme");
removeCookie
<code class="language-typescript">export function removeCookie(key: string): void { document.cookie = `${key}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC;`; }</code>
removeCookie("theme");
2. Hook useCookie
mengintegrasikan fungsi utiliti dengan React's useCookie
untuk pengurusan kuki yang elegan dalam komponen. useState
<code class="language-typescript">import { useState } from "react"; import { getCookie, setCookie, removeCookie } from "@/utils/cookie"; export default function useCookie<T>(key: string, initialValue: T): [T | undefined, (action: T | ((prevState: T) => T)) => void, () => void] { const [value, setValue] = useState(() => getCookie(key) as T || initialValue); // ...rest of the hook implementation }</code>
key
. initialValue
initialValue
fungsi Mekanisme: handleDispatch
Mengendalikan kedua -dua kemas kini nilai langsung dan kemas kini fungsional (untuk transformasi negeri). Ia memastikan penyegerakan cookie.
<code class="language-typescript"> const handleDispatch = (action: T | ((prevState: T) => T)) => { if (typeof action === "function") { setValue((prevState) => { const newValue = (action as (prevState: T) => T)(prevState); setCookie(key, newValue); return newValue; }); } else { setValue(action); setCookie(key, action); } };</code>
<code class="language-typescript">export function setCookie(key: string, value: unknown, expireDays = 1): void { const d = new Date(); d.setTime(d.getTime() + expireDays * 24 * 60 * 60 * 1000); document.cookie = `${key}=${value}; expires=${d.toUTCString()}; path=/`; }</code>
undefined
.Cakuk mengembalikan tatasusunan: nilai semasa, fungsi penghantar dan fungsi jelas.
<code class="language-typescript">export function getCookie(key: string): string | undefined { const name = `${key}=`; const decodedCookie = decodeURIComponent(document.cookie); const ca = decodedCookie.split(";"); for (let i = 0; i < ca.length; i++) { let c = ca[i]; while (c.charAt(0) === " ") { c = c.substring(1); } if (c.indexOf(name) === 0) { return c.substring(name.length, c.length); } } return undefined; }</code>
3. Menggunakan useCookie
Cangkuk
Contoh penggunaan dalam komponen React:
<code class="language-typescript">export function removeCookie(key: string): void { document.cookie = `${key}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC;`; }</code>
theme
disimpan dalam kuki, berterusan merentas sesi dan dikemas kini secara serentak. clearTheme
mengalih keluar kuki dan menetapkan semula keadaan.4. Kelebihan useCookie
Cangkuk
Kesimpulan
Cangkuk useCookie
memudahkan pengurusan kuki dalam React, mempromosikan keselamatan jenis dan kod bersih. Ia memperkemas pengurusan negeri berasaskan kuki, menghapuskan kod berulang.
Atas ialah kandungan terperinci Menguruskan kuki penyemak imbas di React dengan Usecookie Hook. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!