{"id":263012,"date":"2023-03-03T09:04:27","date_gmt":"2023-03-03T01:04:27","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=263012"},"modified":"2023-01-30T12:04:59","modified_gmt":"2023-01-30T04:04:59","slug":"react-hooks-context","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/react-hooks-context.html","title":{"rendered":"React\u4f7f\u7528\u9ad8\u9636\u7ec4\u4ef6\u4e0eHooks\u5b9e\u73b0\u6743\u9650\u62e6\u622a\u6559\u7a0b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u9ad8\u9636\u7ec4\u4ef6\u5c31\u662f\u63a5\u53d7\u4e00\u4e2a\u7ec4\u4ef6\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7ec4\u4ef6\uff08\u529f\u80fd\u589e\u5f3a\u7684\u7ec4\u4ef6\uff09\u7684\u51fd\u6570\u3002\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u9ad8\u9636\u7ec4\u4ef6\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u4e0d\u662f\u7ec4\u4ef6\uff0c\u8fd9\u4e00\u70b9\u4e00\u5b9a\u8981\u6ce8\u610f\uff0c\u672c\u6587\u7ed9\u5927\u5bb6\u5206\u4eabReact\u9ad8\u9636\u7ec4\u4ef6\u4f7f\u7528\u5c0f\u7ed3\uff0c\u4e00\u8d77\u770b\u770b\u5427<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u9ad8\u9636\u7ec4\u4ef6\u662f\u4ee5\u7ec4\u4ef6\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u7ec4\u4ef6\u7684\u51fd\u6570\u3002\u8fd4\u56de\u7684\u7ec4\u4ef6\u628a\u4f20\u8fdb\u53bb\u7684\u7ec4\u4ef6\u8fdb\u884c\u529f\u80fd\u5f3a\u5316\u3002\u901a\u8fc7\u4ee5\u4e0b\u793a\u4f8b\u8fdb\u4e00\u6b65\u7406\u89e3\u9ad8\u9636\u7ec4\u4ef6\u3002<\/p>\n

\u601d\u8def<\/strong><\/div>\n
  • \u4f7f\u7528HOC\u5305\u88f9\u9700\u8981\u6743\u9650\u7684\u9875\u9762\u6216\u7ec4\u4ef6\uff0c\u5e76\u6ce8\u5165\u552f\u4e00\u7684\u6743\u9650\u7b7e\u540d<\/li>\n
  • \u7528Context\u4fdd\u5b58\u5168\u5c40\u7684\u6743\u9650\u83dc\u5355\u5217\u8868\uff0c\u7528Provider\u6ce8\u5165\u5f02\u6b65\u83b7\u53d6\u5230\u7684\u6743\u9650\u83dc\u5355<\/li>\n
  • \u5728HOC\u4e2d\u7528useContext \/ Consumer\u83b7\u53d6\u6743\u9650\u5217\u8868\uff0c\u5e76\u548c\u7b7e\u540d\u505a\u5339\u914d\u3002\u5982\u679c\u6709\u6743\u9650\uff0c\u5219\u5c55\u793a\uff1b\u6ca1\u6709\u6743\u9650\uff0c\u5c55\u793a\u6ca1\u6709\u6743\u9650\u7ec4\u4ef6<\/li>\n
    \u5b9e\u73b0<\/strong><\/div>\n
    \u6ce8\u5165\u6743\u9650\u5217\u8868<\/strong><\/span><\/div>\n

    \u62bd\u79bbContext<\/p>\n

    \u5728\u6587\u4ef6\u4e2d\u521b\u5efa\u4e00\u4e2acontext\uff0c\u5e76\u4f7f\u7528export\u66b4\u9732\u51fa\u5bf9\u5e94\u7684Provider\uff0cConsumer\uff0cContext<\/p>\n

    \/*\r\n * \u8d44\u6e90\u8def\u5f84 .\/src\/utils\/PermissionContext.js\r\n *\/\r\nimport { createContext } from \"react\";\r\nconst PermissionContext = createContext()\r\nexport const PermissionContextProvider = PermissionContext.Provider\r\nexport const PermissionContextConsumer = PermissionContext.Consumer\r\nexport default PermissionContext<\/pre>\n

    \u5411\u9875\u9762\u6ce8\u5165\u6743\u9650\u5217\u8868\u7684HOC<\/p>\n

    \u6b64\u5904\u521b\u5efa\u4e00\u4e2aHOC\uff0c\u5305\u88f9\u4e86App\u7ec4\u4ef6\uff0c\u5b9e\u73b0\u4e86\u5411\u6574\u4e2a\u9875\u9762\u6ce8\u5165Permission\u5217\u8868<\/p>\n

    \/*\r\n * \u8d44\u6e90\u8def\u5f84 .\/src\/components\/PermissionIndex\/PermissionIndex.js\r\n *\/\r\nimport React, { useState, useEffect } from 'react'\r\nimport { PermissionContextProvider } from '..\/..\/utils\/PermissionContext' \/\/ import\u5bf9\u5e94\u7684Context\r\nfunction PermissionIndex(Component) {\r\n  return function Index(props){\r\n    const [permission, setpermission] = useState([])\r\n    useEffect(()=>{\r\n        setpermission(['edituser','checkorder'])\r\n        \/\/\u6b64\u5904\u5b9e\u9645\u4e3a \u83b7\u53d6\u6743\u9650\u5217\u8868\u7684\u8bf7\u6c42\u64cd\u4f5c\r\n    },[])\r\n    \/\/\u4ee3\u66ff\u4e86\u7c7b\u7ec4\u4ef6\u7684componenetDidMount\u751f\u547d\u5468\u671f\r\n    return (\r\n      < PermissionContextProvider value={permission}>\r\n        < Component {...props}>< \/Component>\r\n      < \/PermissionContextProvider>\r\n      \/\/\u6b64\u5904\u8fd4\u56de\u4e86\u6ce8\u5165\u6743\u9650\u5217\u8868Context\u7684\u7ec4\u4ef6\r\n    )\r\n  }\r\n}\r\nexport default PermissionIndex<\/pre>\n
  • useState\u7528\u4e8e\u52a8\u6001\u6ce8\u5165\u83b7\u53d6\u7684\u6743\u9650\u5217\u8868<\/li>\n
  • \u7ec4\u4ef6\u901a\u8fc7Context.Provider\u5305\u88f9\u3002\u6743\u9650\u5217\u8868\u6539\u53d8\uff0c\u6240\u6709\u6d88\u8d39\u6743\u9650\u5217\u8868\u7684\u7ec4\u4ef6\u91cd\u65b0\u66f4\u65b0<\/li>\n

    \u5411\u6839\u7ec4\u4ef6\u6ce8\u5165\u6743\u9650<\/p>\n

    import React from 'react';\r\nimport ReactDOM from 'react-dom';\r\nimport '.\/index.css';\r\nimport App from '.\/pages\/App\/App'\r\nimport PermissionIndex from '.\/components\/PermissionIndex\/PermissionIndex'\r\nconst IndexWithPermission = PermissionIndex(App)\r\nReactDOM.render(\r\n  < IndexWithPermission\/>,\r\n  document.getElementById('root')\r\n);<\/pre>\n
    \u542b\u6709\u6743\u9650\u62e6\u622a\u529f\u80fd\u7684HOC<\/strong><\/span><\/div>\n

    \u65e0\u6743\u9650\u65f6\u663e\u793a\u7684\u7ec4\u4ef6<\/p>\n

    import React from 'react'\r\nfunction NoPermission() {\r\n  return (\r\n    < div>\u5bf9\u4e0d\u8d77\uff0c\u8bf7\u4e0e\u7ba1\u7406\u5458\u83b7\u53d6\u6743\u9650< \/div>\r\n  )\r\n}\r\nexport default NoPermission<\/pre>\n

    \u6743\u9650\u62e6\u622aHOC\u7ec4\u4ef6<\/p>\n

    \/*\r\n * \u8d44\u6e90\u8def\u5f84 .\/src\/components\/PermissionHOC\/PermissionHOC.js\r\n *\/\r\nimport React, { useContext } from 'react'\r\nimport PermissionContext from '..\/..\/utils\/PermissionContext'\r\nimport NoPermission from '..\/NoPermission\/NoPermission'\r\nfunction PermissionHoc(authorization) {\r\n  return function(Component){\r\n    return function PermissionIndex(props){\r\n      const context = useContext(PermissionContext)\r\n      \/\/\u4f7f\u7528useContext\u83b7\u53d6\u5f53\u524d\u7684\u6743\u9650\u5217\u8868\uff0c\u76f8\u5f53\u4e8e\u7c7b\u7ec4\u4ef6\u4e2d\u7684contentType\u9759\u6001\u5c5e\u6027\r\n      return context.indexOf(authorization) >= 0 ? < Component {...props}>< \/Component> : < NoPermission>< \/NoPermission>\r\n    }\r\n  }\r\n}\r\nexport default PermissionHoc<\/pre>\n
  • \u6b64\u793a\u4f8b\u4e2d\u4f7f\u7528\u4e24\u5c42\u5305\u88c5\u51fd\u6570\u7684HOC\uff0c\u7b2c\u4e00\u5c42\u7528\u4e8e\u83b7\u53d6HOC\u7ed1\u5b9a\u7684\u5f53\u524d\u7ec4\u4ef6\u7684\u6743\u9650\u7b7e\u540d\uff0c\u56e0\u4e3a\u8981\u7528\u8fd9\u4e2a\u6743\u9650\u7b7e\u540d\u548c\u6743\u9650\u5217\u8868\u505a\u5339\u914d\u3002\u7b2c\u4e8c\u5c42\u63a5\u53d7\u539f\u59cb\u7ec4\u4ef6\u3002<\/li>\n
  • \u5728HOC\u4e2d\u4f7f\u7528 useContext \u63a5\u6536\u6743\u9650\u5217\u8868\uff0c\u505a\u6743\u9650\u5339\u914d\u3002\u7ec4\u4ef6\u6709\u6743\u9650\u5219\u5c55\u793a\uff0c\u6ca1\u6709\u6743\u9650\u5219\u5c55\u793a\u65e0\u6743\u9650\u7ec4\u4ef6\u3002<\/li>\n
    \u6d4b\u8bd5<\/strong><\/span><\/div>\n

    \u7528\u4e8e\u6d4b\u8bd5\u7684\u7ec4\u4ef6<\/p>\n

    \/*\r\n * \u8d44\u6e90\u8def\u5f84 .\/src\/components\/PermissionTest\/PermissionTest.js\r\n *\/\r\nimport React from 'react'\r\nfunction PermissionTest(props) {\r\n  return (\r\n    < div>PermissionTest - {props?.name}< \/div>\r\n  )\r\n}\r\nexport default PermissionTest<\/pre>\n

    \u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528\u6743\u9650\u7ec4\u4ef6<\/p>\n

    \/*\r\n * \u8d44\u6e90\u8def\u5f84 .\/src\/components\/App\/App.js\r\n *\/\r\nimport React, { useContext, useEffect, useRef } from 'react'\r\nimport PermissionContext from '..\/..\/utils\/PermissionContext'\r\nimport PermissionHoc from '..\/..\/components\/PermissionHoc\/PermissionHoc'\r\nimport PermissionTest from '..\/..\/components\/PermissionTest\/PermissionTest'\r\nexport default function App() {\r\n  const Edituser = PermissionHoc('edituser')(PermissionTest)\r\n  const Edituser1 = PermissionHoc('edituser1')(PermissionTest)\r\n  return (\r\n    < div>\r\n        < Edituser name=\"edituser\">< \/Edituser>\r\n        < Edituser1 name=\"edituser1\">< \/Edituser1>\r\n    < \/div>\r\n  )\r\n}<\/pre>\n

    \u6b64\u5904\uff0c\u901a\u8fc7\u7528PermissionHOC\u51fd\u6570\u5904\u7406\u539f\u59cb\u7ec4\u4ef6\uff0c\u8fdb\u884c\u4e86\u6743\u9650\u62e6\u622a\u529f\u80fd<\/p>\n

    \"\"<\/a><\/p>\n

    \u5230\u6b64\u8fd9\u7bc7\u5173\u4e8eReact\u4f7f\u7528\u9ad8\u9636\u7ec4\u4ef6\u4e0eHooks\u5b9e\u73b0\u6743\u9650\u62e6\u622a\u6559\u7a0b\u8be6\u7ec6\u5206\u6790\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u9ad8\u9636\u7ec4\u4ef6\u662f\u4ee5\u7ec4\u4ef6\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u7ec4\u4ef6\u7684\u51fd\u6570\u3002\u8fd4\u56de\u7684\u7ec4\u4ef6\u628a\u4f20\u8fdb\u53bb\u7684\u7ec4\u4ef6\u8fdb\u884c\u529f\u80fd\u5f3a\u5316\u3002\u901a\u8fc7\u4ee5\u4e0b\u793a\u4f8b\u8fdb\u4e00\u6b65\u7406\u89e3\u9ad8\u9636 […]<\/p>\n","protected":false},"author":1482,"featured_media":34397,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-263012","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/263012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=263012"}],"version-history":[{"count":7,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/263012\/revisions"}],"predecessor-version":[{"id":263020,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/263012\/revisions\/263020"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/34397"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=263012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=263012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=263012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}