{"id":52276,"date":"2024-12-03T14:22:37","date_gmt":"2024-12-03T06:22:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52276\/"},"modified":"2024-12-03T14:22:37","modified_gmt":"2024-12-03T06:22:37","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87react-cookie-consent%e5%9c%a8react%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e4%b8%ad%e4%bd%bf%e7%94%a8cookie-consent","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52276\/","title":{"rendered":"\u5982\u4f55\u901a\u8fc7react-cookie-consent\u5728React\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528Cookie Consent"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u901a\u8fc7react-cookie-consent\u5728React\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528Cookie Consent<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300a\u5982\u4f55\u901a\u8fc7react-cookie-consent\u5728React\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528Cookie Consent\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1732020155673c87bbb2136.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u901a\u8fc7react-cookie-consent\u5728React\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528Cookie Consent\u63d2\u56fe\" alt=\"\u5982\u4f55\u901a\u8fc7react-cookie-consent\u5728React\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528Cookie Consent\u63d2\u56fe\" \/><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><br \/> \u9690\u79c1\u548c\u6570\u636e\u4fdd\u62a4\u662f\u73b0\u4ee3\u7f51\u7ad9\u7684\u57fa\u672c\u8003\u8651\u56e0\u7d20\uff0c\u56e0\u6b64\u5728 react \u5e94\u7528\u7a0b\u5e8f\u4e2d\u6dfb\u52a0 cookie \u540c\u610f\u6a2a\u5e45\u53ef\u786e\u4fdd\u9075\u5b88\u6570\u636e\u9690\u79c1\u6cd5\u89c4\uff0c\u4f8b\u5982 gdpr\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528react-cookie-consent\u5e93\u8f7b\u677e\u5730\u5c06cookie\u540c\u610f\u6a2a\u5e45\u6dfb\u52a0\u5230\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u81ea\u5b9a\u4e49\u4ee5\u4f7f\u7528\u6237\u80fd\u591f\u63a7\u5236cookie\u9996\u9009\u9879\u3002<\/p>\n<p><strong>\u7b2c 1 \u6b65\uff1a\u8bbe\u7f6e\u4f60\u7684 react \u9879\u76ee<\/strong><\/p>\n<p>\u5982\u679c\u60a8\u5c1a\u672a\u8bbe\u7f6e react \u9879\u76ee\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\uff1a<\/p>\n<pre>npx create-react-app cookie-consent-demo\ncd cookie-consent-demo\n<\/pre>\n<p><strong>\u7b2c2\u6b65\uff1a\u5b89\u88c5react-cookie-consent<\/strong><\/p>\n<p>react-cookie-consent \u5e93\u7b80\u5316\u4e86\u5411\u60a8\u7684\u5e94\u7528\u6dfb\u52a0 cookie \u540c\u610f\u6a2a\u5e45\u7684\u8fc7\u7a0b\u3002\u4f7f\u7528npm\u6216yarn\u5b89\u88c5\u5b83\uff1a<\/p>\n<pre>npm install react-cookie-consent\n# or\nyarn add react-cookie-consent\n<\/pre>\n<p><strong>\u7b2c 3 \u6b65\uff1a\u57fa\u672c\u5b9e\u65bd<\/strong><\/p>\n<p>\u5b89\u88c5\u5e93\u540e\uff0c\u8f6c\u5230\u4e3b\u5e94\u7528\u7a0b\u5e8f\u6587\u4ef6\uff08\u901a\u5e38\u662f app.js \u6216 app.tsx\uff09\u5e76\u6dfb\u52a0 cookie \u540c\u610f\u7ec4\u4ef6\u3002\u8fd9\u662f\u57fa\u672c\u8bbe\u7f6e\uff1a<\/p>\n<pre>import react from \"react\";\nimport cookieconsent from \"react-cookie-consent\";\n\nfunction app() {\n  return (\n    &lt;div classname=\"app\"&gt;\n      &lt;h1&gt;welcome to my website&lt;\/h1&gt;\n      {\/* basic cookie consent *\/}\n      &lt;cookieconsent\n        location=\"bottom\"\n        buttontext=\"i understand\"\n        cookiename=\"mywebsitecookieconsent\"\n        style={{ background: \"#2b373b\" }}\n        buttonstyle={{ color: \"#4e503b\", fontsize: \"13px\" }}\n        expires={150}\n      &gt;\n        this website uses cookies to enhance your browsing experience.{\" \"}\n        &lt;a href=\"\/privacy-policy\" style={{ color: \"#f5e042\" }}&gt;\n          learn more\n        &lt;\/a&gt;\n      &lt;\/cookieconsent&gt;\n    &lt;\/div&gt;\n  );\n}\n\nexport default app;\n<\/pre>\n<p><strong>\u7b2c 4 \u6b65\uff1a\u81ea\u5b9a\u4e49\u540c\u610f\u6a2a\u5e45<\/strong><\/p>\n<p>\u8981\u4f7f\u540c\u610f\u6a2a\u5e45\u66f4\u5177\u5438\u5f15\u529b\u6216\u6dfb\u52a0\u201c\u5168\u90e8\u63a5\u53d7\u201d\u548c\u201c\u62d2\u7edd\u201d\u7b49\u9009\u9879\uff0c\u60a8\u53ef\u4ee5\u6269\u5c55\u8be5\u7ec4\u4ef6\u3002<\/p>\n<pre>&lt;CookieConsent\n  location=\"bottom\"\n  enableDeclineButton\n  declineButtonText=\"Decline\"\n  buttonText=\"Accept All\"\n  cookieName=\"myWebsiteCookieConsent\"\n  style={{ background: \"#000\" }}\n  buttonStyle={{ color: \"#fff\", fontSize: \"14px\" }}\n  declineButtonStyle={{\n    color: \"#fff\",\n    background: \"#c0392b\",\n    fontSize: \"14px\",\n  }}\n  expires={365}\n  onAccept={() =&gt; {\n    console.log(\"Cookies accepted\");\n  }}\n  onDecline={() =&gt; {\n    console.log(\"Cookies declined\");\n  }}\n&gt;\n  We use cookies to improve user experience. By clicking \"Accept All,\" you consent to the use of cookies.\n&lt;\/CookieConsent&gt;\n<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><br \/> \u6dfb\u52a0 cookie \u540c\u610f\u6a2a\u5e45\u5bf9\u4e8e\u7528\u6237\u9690\u79c1\u548c\u6cd5\u89c4\u9075\u4ece\u6027\u81f3\u5173\u91cd\u8981\uff0c\u800c react-cookie-consent \u4f7f\u5176\u6613\u4e8e\u5b9e\u73b0\u3002\u60a8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u81ea\u5b9a\u4e49\u6a2a\u5e45\uff0c\u4ee5\u7b26\u5408\u60a8\u7f51\u7ad9\u7684\u8bbe\u8ba1\u548c\u6d88\u606f\u4f20\u9012\uff0c\u5e76\u54cd\u5e94\u7528\u6237\u7684\u9009\u62e9\uff0c\u4ee5\u589e\u5f3a\u5bf9\u57fa\u4e8e cookie \u7684\u529f\u80fd\u7684\u63a7\u5236\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u901a\u8fc7react-cookie-consent\u5728React\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528Cookie Consent\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   Win11\u7cfb\u7edf\u51fa\u73b0ERR_ADDRESS_UNREACHABLE\u9519\u8bef\u7684\u89e3\u51b3\u65b9\u6cd5\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u901a\u8fc7react-cookie-consent\u5728React\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528Cookie Consent \u6536\u85cf \u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300a\u5982\u4f55\u901a\u8fc7react-cookie-consent\u5728React\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528Cookie Consent\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01 \u7b80\u4ecb \u9690\u79c1\u548c\u6570\u636e\u4fdd\u62a4\u662f\u73b0\u4ee3\u7f51\u7ad9\u7684\u57fa\u672c\u8003\u8651\u56e0\u7d20\uff0c\u56e0\u6b64\u5728 react \u5e94\u7528\u7a0b\u5e8f\u4e2d\u6dfb\u52a0 cookie \u540c\u610f\u6a2a\u5e45\u53ef\u786e\u4fdd\u9075\u5b88\u6570\u636e\u9690\u79c1\u6cd5\u89c4\uff0c\u4f8b\u5982 gdpr\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528react-cookie-consent\u5e93\u8f7b\u677e\u5730\u5c06cookie\u540c\u610f\u6a2a\u5e45\u6dfb\u52a0\u5230\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u81ea\u5b9a\u4e49\u4ee5\u4f7f\u7528\u6237\u80fd\u591f\u63a7\u5236cookie\u9996\u9009\u9879\u3002 \u7b2c 1 \u6b65\uff1a\u8bbe\u7f6e\u4f60\u7684 react \u9879\u76ee \u5982\u679c\u60a8\u5c1a\u672a\u8bbe\u7f6e react \u9879\u76ee\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\uff1a npx create-react-app cookie-consent-demo cd cookie-consent-demo \u7b2c2\u6b65\uff1a\u5b89\u88c5react-cookie-consent react-cookie-consent \u5e93\u7b80\u5316\u4e86\u5411\u60a8\u7684\u5e94\u7528\u6dfb\u52a0 cookie \u540c\u610f\u6a2a\u5e45\u7684\u8fc7\u7a0b\u3002\u4f7f\u7528npm\u6216yarn\u5b89\u88c5\u5b83\uff1a npm install react-cookie-consent # or yarn add react-cookie-consent \u7b2c 3 \u6b65\uff1a\u57fa\u672c\u5b9e\u65bd \u5b89\u88c5\u5e93\u540e\uff0c\u8f6c\u5230\u4e3b\u5e94\u7528\u7a0b\u5e8f\u6587\u4ef6\uff08\u901a\u5e38\u662f app.js \u6216 app.tsx\uff09\u5e76\u6dfb\u52a0 cookie \u540c\u610f\u7ec4\u4ef6\u3002\u8fd9\u662f\u57fa\u672c\u8bbe\u7f6e\uff1a import react from &#8220;react&#8221;; import cookieconsent from &#8220;react-cookie-consent&#8221;; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-52276","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=52276"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52276\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}