{"id":2443,"date":"2021-11-04T07:28:49","date_gmt":"2021-11-03T22:28:49","guid":{"rendered":"https:\/\/nobunobu1717.site\/?p=2443"},"modified":"2021-11-04T07:28:49","modified_gmt":"2021-11-03T22:28:49","slug":"post-2443","status":"publish","type":"post","link":"https:\/\/nobunobu1717.site\/?p=2443","title":{"rendered":"\u3010React\u3011\u30ed\u30b0\u30a4\u30f3\u5f8c\u306b\u30ea\u30ed\u30fc\u30c9\u3059\u308b\u3068\u30ed\u30b0\u30a4\u30f3\u753b\u9762\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\u3066\u3057\u307e\u3046\u554f\u984c\u3068\u89e3\u6c7a\u65b9\u6cd5\u30e1\u30e2"},"content":{"rendered":"<h2>\u6982\u8981<\/h2>\n<p>React\u3067\u30ed\u30b0\u30a4\u30f3\u6a5f\u80fd\u3092\u5b9f\u88c5\u3057\u305f\u969b\u306b\u3001\u753b\u9762\u3092\u30ea\u30ed\u30fc\u30c9\u3059\u308b\u3068\u6bce\u56de\u30ed\u30b0\u30a4\u30f3\u753b\u9762\u304c\u8868\u793a\u3055\u308c\u3066\u3057\u307e\u3046\u75c7\u72b6\u304c\u767a\u751f\u3057\u305f\u306e\u3067\u5bfe\u7b56\u3068\u30e1\u30e2\u3067\u3059\u3002<\/p>\n\n\t<p style=\"color:#666;margin-bottom:5px;\">\u30b9\u30dd\u30f3\u30b5\u30fc\u30ea\u30f3\u30af<\/p>\n\t<div class=\"middle-ad\">\n\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\n\n<h3>\u75c7\u72b6<\/h3>\n<p>React (React Hook)\u3092\u4f7f\u7528\u3057\u305f\u30ed\u30b0\u30a4\u30f3\u6a5f\u80fd\u3092Router\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3057\u3001\u8a8d\u8a3c\u6e08\u307f\u306e\u5834\u5408\u306e\u307f\u30a2\u30af\u30bb\u30b9\u53ef\u80fd\u306a\u30da\u30fc\u30b8\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\n\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30ab\u30b9\u30bf\u30e0\u3057\u305fRouter(UserAuthRoute)\u3092\u4f5c\u308a\u3001\u3053\u308c\u3092\u4f7f\u7528\u3057\u305f\u30da\u30fc\u30b8\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306f\u8a8d\u8a3c\u3057\u306a\u3044\u3068\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3059\u3002<br \/>\n\u5b9f\u88c5\u306f\u5f8c\u8ff0\u3057\u307e\u3059\u304c\u3001\u3053\u306e\u3088\u3046\u306a\u6a5f\u80fd\u3092\u5b9f\u88c5\u3057\u305f\u72b6\u614b\u3067\u3001\u30ed\u30b0\u30a4\u30f3\u5f8c\u306b\u753b\u9762\u3092\u30ea\u30ed\u30fc\u30c9\u3059\u308b\u3068\u5fc5\u305a\u30ed\u30b0\u30a4\u30f3\u753b\u9762\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u3001\u305d\u306e\u5f8c\u81ea\u52d5\u3067\u30ed\u30b0\u30a4\u30f3\u3055\u308c\u3066\u30c8\u30c3\u30d7\u30da\u30fc\u30b8\u306b\u623b\u308b\u3068\u3044\u3046\u75c7\u72b6\u304c\u767a\u751f\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:default decode:true \">export const App = () =&gt; (\r\n...\r\n    &lt;BrowserRouter&gt;\r\n    ...\r\n     &lt;Switch&gt;\r\n      &lt;Route exact path=\"\/\" component={Home}&gt;&lt;\/Route&gt;\r\n      &lt;UserLoginRoute path=\"\/user_login\" component={UserLogin}&gt;&lt;\/UserLoginRoute&gt;\r\n      &lt;UserAuthRoute path=\"\/greeting\" component={Greeting} &gt;&lt;\/UserAuthRoute\r\n     ...\r\n    &lt;\/Switch&gt;\r\n   &lt;BrowserRouter&gt;\r\n...\r\n);<\/pre>\n<p>UserAuthRoute\u304c\u3001\u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\u304c\u7121\u3044\u3068\u30ed\u30b0\u30a4\u30f3\u30da\u30fc\u30b8\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3059\u308bRouter\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\u8a8d\u8a3c\u72b6\u614b\u306f\u3001UserAuthContext\u3068\u3044\u3063\u305f\u30ab\u30b9\u30bf\u30e0\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u4f5c\u3063\u3066\u305d\u3061\u3089\u3067\u7ba1\u7406\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">import React from \"react\";\r\nimport { Route, Redirect } from \"react-router-dom\";\r\nimport { useUserAuth } from \"..\/..\/contexts\/UserAuthContext\";\r\n\r\nexport default function UserAuthRoute({ component: Component, ...rest }: any) {\r\n  const { state } = useUserAuth();\r\n\r\n  return (\r\n    &lt;Route\r\n      {...rest}\r\n      render={(props) =&gt; {\r\n        return state.isAuthrized ? (\r\n          &lt;Component {...props} \/&gt;\r\n        ) : (\r\n          &lt;Redirect to={\"user_login\" + props.location?.pathname} \/&gt;\r\n        );\r\n      }}\r\n    &gt;&lt;\/Route&gt;\r\n  );\r\n}<\/pre>\n<p>\u30ed\u30b0\u30a4\u30f3\u30da\u30fc\u30b8\u306b\u4f7f\u7528\u3057\u3066\u3044\u308b\u3001UserLoginRoute\u306f\u8a8d\u8a3c\u6e08\u307f\u3067\u3042\u308c\u3070\u3001\u30ed\u30b0\u30a4\u30f3\u30da\u30fc\u30b8\u306b\u30a2\u30af\u30bb\u30b9\u3055\u305b\u306a\u3044\u3088\u3046\u306b\u30ed\u30b0\u30a4\u30f3\u6e08\u307f\u3067\u3042\u308c\u3070\u30c8\u30c3\u30d7\u30da\u30fc\u30b8\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">import React from 'react'\r\nimport { Route, Redirect } from 'react-router-dom'\r\nimport { useUserAuth } from \"..\/..\/contexts\/UserAuthContext\"\r\n\r\nexport default function LoginRoute({ component: Component, ...rest }: any) {\r\n  const { state } = useUserAuth()\r\n\r\n  return (\r\n    &lt;Route\r\n      {...rest}\r\n      render={(props) =&gt; {\r\n        return state.isAuthrized ? &lt;Redirect to=\"\/\" \/&gt; : &lt;Component {...props} \/&gt;\r\n      }}\r\n    &gt;&lt;\/Route&gt;\r\n  )\r\n}\r\n<\/pre>\n\n\t<p style=\"color:#666;margin-bottom:5px;\">\u30b9\u30dd\u30f3\u30b5\u30fc\u30ea\u30f3\u30af<\/p>\n\t<div class=\"middle-ad\">\n\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\n\n<h3>\u539f\u56e0<\/h3>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30ed\u30b0\u30a4\u30f3\u72b6\u614b\u3092\u7ba1\u7406\u3059\u308bContextProvider\u306b\u304a\u3044\u3066\u3001\u521d\u671f\u30ed\u30fc\u30c9\u72b6\u614b\u3092\u5b9a\u671f\u3059\u308bloading\u3092\u4f7f\u7528\u3057\u3066\u63cf\u753b\u3092\u6b62\u3081\u3066\u3044\u306a\u3044\u4e8b\u304c\u539f\u56e0\u3067\u3057\u305f\u3002<br \/>\n{!loading &amp;&amp; children}<br \/>\n\u3068\u3044\u3063\u305f\u5f62\u3067\u3001loading\u304ctrue\u306b\u306a\u308b\u307e\u3067\u5b50\u306e\u63cf\u753b\u3092\u884c\u308f\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u4e8b\u3067\u89e3\u6c7a\u3057\u307e\u3057\u305f\u3002<br \/>\n\u3053\u308c\u3092\u884c\u308f\u306a\u3044\u3068\u8d77\u52d5\u76f4\u5f8c\u306e\u8a8d\u8a3c\u304c\u7d42\u308f\u308b\u524d\u306b\u3001UserAuthRoute\u306a\u3069\u3067\u8a8d\u8a3c\u72b6\u614b\u306e\u30c1\u30a7\u30c3\u30af\u304c\u884c\u308f\u308c\u3066\u3057\u307e\u3044\u3001\u7d50\u679c\u3068\u3057\u3066\u672a\u8a8d\u8a3c\u3068\u3057\u3066\u30ed\u30b0\u30a4\u30f3\u30da\u30fc\u30b8\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\u3066\u3057\u307e\u3046\u3068\u3044\u3046\u75c7\u72b6\u304c\u51fa\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:default decode:true \">import * as React from \"react\";\r\nimport { createContext, useContext, useState, useEffect } from \"react\";\r\nimport { UserAuthResult, UserAuthService } from \"..\/lib\/UserAuth\";\r\n\r\n\r\n\/\/ data define\r\ntype StateType = {\r\n  isAuthrized: boolean;\r\n};\r\n\r\ntype ContextType = {\r\n  state: StateType;\r\n  login: (passCode: string) =&gt; UserAuthResult;\r\n  logout: () =&gt; void;\r\n};\r\n\r\nconst initialState = { isAuthrized: false };\r\n\r\nconst UserAuthContext = createContext({} as ContextType);\r\n\r\nexport function useUserAuth(): ContextType {\r\n  return useContext(UserAuthContext);\r\n}\r\n\r\n\r\nexport const UserAuthProvider = ({ children }: any) =&gt; {\r\n  const [state, setState] = useState(initialState);\r\n  const [loading, setLoading] = useState(false);\r\n\r\n  function login(id: string, passCode: string): UserAuthResult {\r\n    \/\/ \u3053\u3053\u306b\u30ed\u30b0\u30a4\u30f3\u8a8d\u8a3c\u3092\u5b9f\u88c5\r\n    ...\r\n\r\n    setState({ ...state, isAuthrized: result.isSucessful });\r\n    return result;\r\n  }\r\n\r\n  function logout(): void {\r\n    \/\/ \u3053\u3053\u306b\u30ed\u30b0\u30a2\u30a6\u30c8\u3092\u5b9f\u88c5\r\n    ...\r\n    setState({ ...state, isAuthrized: false });\r\n  }\r\n\r\n  useEffect(() =&gt; {\r\n    setLoading(true);\r\n\r\n    \/\/ \u3053\u3053\u306b\u521d\u671f\u8a8d\u8a3c\u3092\u5b9f\u65bd\r\n    ...\r\n\r\n    setLoading(false);\r\n  }, []);\r\n\r\n  const values = {\r\n    state,\r\n    login,\r\n    logout,\r\n  };\r\n\r\n  return (\r\n    &lt;UserAuthContext.Provider value={values}&gt;\r\n      {!loading &amp;&amp; children}\r\n    &lt;\/UserAuthContext.Provider&gt;\r\n  );\r\n};\r\n<\/pre>\n<h3>\u304a\u307e\u3051<\/h3>\n<p>\u4e0a\u8a18\u306e\u3088\u3046\u306a\u5b9f\u88c5\u3092\u884c\u3046\u3068\u3001\u521d\u671f\u8a8d\u8a3c\u6642\u306b\u753b\u9762\u304c\u771f\u3063\u767d\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u753b\u9762\u3092\u5165\u308c\u305f\u65b9\u304c\u826f\u3044\u3067\u3059\u3002<br \/>\n(loding\u306e\u6642\u306b\u306f\u5c02\u7528\u306ehtml\u8981\u7d20\u3092\u8fd4\u3059\u5f62\u3067\u3059\u3002)<\/p>\n<pre class=\"lang:default decode:true \">import * as React from \"react\";\r\nimport { createContext, useContext, useState, useEffect } from \"react\";\r\nimport { UserAuthResult, UserAuthService } from \"..\/lib\/UserAuth\";\r\n\r\n\r\n\/\/ data define\r\ntype StateType = {\r\n  isAuthrized: boolean;\r\n};\r\n\r\ntype ContextType = {\r\n  state: StateType;\r\n  login: (passCode: string) =&gt; UserAuthResult;\r\n  logout: () =&gt; void;\r\n};\r\n\r\nconst initialState = { isAuthrized: false };\r\n\r\nconst UserAuthContext = createContext({} as ContextType);\r\n\r\nexport function useUserAuth(): ContextType {\r\n  return useContext(UserAuthContext);\r\n}\r\n\r\n\r\nexport const UserAuthProvider = ({ children }: any) =&gt; {\r\n  const [state, setState] = useState(initialState);\r\n  const [loading, setLoading] = useState(false);\r\n\r\n  ...\r\n\r\n  if (loading) {\r\n    return (\/\/ \u3053\u3053\u306b\u30b9\u30d4\u30ca\u30fc\u7b49\u306e\u3092\u4f5c\u6210&lt;&gt;Loading...&lt;\/&gt;)\r\n\r\n  return (\r\n    &lt;UserAuthContext.Provider value={values}&gt;\r\n      {!loading &amp;&amp; children}\r\n    &lt;\/UserAuthContext.Provider&gt;\r\n  );\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 React\u3067\u30ed\u30b0\u30a4\u30f3\u6a5f\u80fd\u3092\u5b9f\u88c5\u3057\u305f\u969b\u306b\u3001\u753b\u9762\u3092\u30ea\u30ed\u30fc\u30c9\u3059\u308b\u3068\u6bce\u56de\u30ed\u30b0\u30a4\u30f3\u753b\u9762\u304c\u8868\u793a\u3055\u308c\u3066\u3057\u307e\u3046\u75c7\u72b6\u304c\u767a\u751f\u3057\u305f\u306e\u3067\u5bfe\u7b56\u3068\u30e1\u30e2\u3067\u3059\u3002 \u30b9\u30dd\u30f3\u30b5\u30fc\u30ea\u30f3\u30af \u75c7\u72b6 React (React Hook)\u3092\u4f7f &#8230; <\/p>\n","protected":false},"author":1,"featured_media":2450,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22,47,16],"tags":[],"_links":{"self":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts\/2443"}],"collection":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2443"}],"version-history":[{"count":5,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts\/2443\/revisions"}],"predecessor-version":[{"id":2499,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts\/2443\/revisions\/2499"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/media\/2450"}],"wp:attachment":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}