{"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":"
\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 \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\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 |