{"id":15405,"date":"2019-10-21T09:00:44","date_gmt":"2019-10-21T00:00:44","guid":{"rendered":"https:\/\/www.agent-grow.com\/self20percent\/?p=15405"},"modified":"2019-10-15T10:34:29","modified_gmt":"2019-10-15T01:34:29","slug":"react-typescript-decorator-wrapper","status":"publish","type":"post","link":"https:\/\/www.agent-grow.com\/self20percent\/2019\/10\/21\/react-typescript-decorator-wrapper\/","title":{"rendered":"TypeScript \u306e Decorator (HOC \u3082)\u3092\u4f7f\u3063\u3066\u3001React \u306e\u90e8\u54c1\u3092\u4f7f\u3044\u56de\u3057\u305f\u3044\uff01"},"content":{"rendered":"<h1>React \u306e\u63cf\u753b\u5185\u5bb9\u3092 Decorator \u3067\u30e9\u30c3\u30d4\u30f3\u30b0\u3057\u3066\u307f\u308b\uff01<\/h1>\n<p>React \u66f8\u3044\u3066\u307e\u3059\u304b\uff1f<\/p>\n<p>\u6700\u8fd1\u79c1\u306f Vue \u306e\u65b9\u304c\u304a\u597d\u307f\u3067\u3059\u304c\u3001<br \/>\n\u3084\u3063\u3071\u308a\u8907\u96d1\u306a\u4e8b\u3092\u3057\u3088\u3046\u3068\u601d\u3046\u3068\u3001<br \/>\nJavaScript \u3092\u76f4\u63a5\u62e1\u5f35\u3057\u3066\u3044\u308b\u3088\u3046\u306a React \u306e\u66f8\u304d\u5473\u306f\u306a\u3093\u3068\u3082\u5fc3\u5730\u3088\u3044\u3082\u306e\u3067\u3059\u306d\u3002<\/p>\n<p>\u3067\u3001\u5148\u65e5 React \u3092\u66f8\u3044\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001<br \/>\nNuxt \u306e layout \u30d7\u30ed\u30d1\u30c6\u30a3\u306e\u3088\u3046\u306b\u3001\u30e1\u30cb\u30e5\u30fc\u306a\u3069\u306e\u5171\u901a\u3067\u4f7f\u3046\u90e8\u54c1\u3092\u5916\u90e8\u304b\u3089\u5dee\u3057\u8fbc\u307f\u305f\u3044\u306a\u3068\u601d\u3044\u307e\u3057\u3066\u3002<\/p>\n<p>TypeScript \u3067\u66f8\u3044\u3066\u3044\u305f\u306e\u3067\u3001 Decorator \u3067\u66f8\u3051\u3070\u826f\u3044\u3058\u3083\u3093\uff01<br \/>\n\u3068\u3044\u3046\u3053\u3068\u3067\u3084\u3063\u3066\u307f\u305f\u8a18\u9332\u3067\u3059\u3002<\/p>\n<h1>Decorator \u3067\u30b3\u30f3\u30c6\u30f3\u30c4\u30e9\u30c3\u30d1\u30fc\u3092\u4f5c\u308b<\/h1>\n<h2>\u3084\u308a\u305f\u3044\u3053\u3068<\/h2>\n<p>\u3084\u308a\u305f\u3044\u3053\u3068\u306f\u3053\u308c\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst menu: FC = ({ children }) =\uff1e (\n\uff1cdiv\uff1e\n  \uff1cul\uff1e\n    \uff1cli\uff1e\u9805\u76ee1\uff1c\/li\uff1e\n  \uff1c\/ul\uff1e\n  { children }\n\uff1c\/div\uff1e\n);\n\nconst main: FC = () =\uff1e (\n  \uff1cMenu\uff1e\n    \uff1cp\uff1emain contents\uff1c\/p\uff1e\n  \uff1c\/Menu\uff1e\n);\n<\/pre>\n<p>\u3053\u3046\u3059\u308b\u3068\u3001main \u304c menu \u306e <code>children<\/code> \u3067\u5c55\u958b\u3055\u308c\u308b\u306e\u3067\u3001<br \/>\n\u30e1\u30a4\u30f3\u30b3\u30f3\u30c6\u30f3\u30c4\u306b\u30e1\u30cb\u30e5\u30fc\u3092\u304f\u3063\u3064\u3051\u3066\u8868\u793a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u306d\u3002<\/p>\n<p>\u305f\u3060\u3001\u3053\u308c\u3060\u3068\u30e1\u30cb\u30e5\u30fc\u306e\u4e2d\u306b\u30e1\u30a4\u30f3\u30b3\u30f3\u30c6\u30f3\u30c4\u304c\u3042\u308b\u307f\u305f\u3044\u306a DOM \u69cb\u6210\u306b\u306a\u3063\u3066<br \/>\n\u3061\u3087\u3063\u3068\u6c17\u6301\u3061\u60aa\u3044\u611f\u3058\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u3053\u306e menu \u3092\u3001\u30c7\u30b3\u30ec\u30fc\u30bf\u30fc\u3067\u3069\u3046\u306b\u304b\u304f\u3063\u3064\u3051\u305f\u3044\u3068\u8a00\u3046\u306e\u304c\u4eca\u56de\u306e\u304a\u6c17\u6301\u3061\u3002<\/p>\n<h2>\u30c7\u30b3\u30ec\u30fc\u30bf\u30fc\u3067\u66f8\u304d\u76f4\u3057\u305f Ver<\/h2>\n<p>\u3055\u3063\u304d\u306f\u7701\u7565\u3057\u307e\u3057\u305f\u3051\u3069\u3001\u3068\u308a\u3042\u3048\u305a HTML \u5074\u306b\u3053\u3093\u306a\u611f\u3058\u306e\u30bf\u30b0\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u60f3\u5b9a\u3092\u3057\u3066\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\uff1cdiv id=\u201dapp\u201d\uff1e\uff1c\/div\uff1e\n<\/pre>\n<p>\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u306f\u3053\u3061\u3089\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n@menu\nclass App extends React.Component {\n  render() {\n    return (\n      \uff1ch1\uff1e\n        main\n      \uff1c\/h1\uff1e\n    );\n  }\n}\n\nReactDOM.render(\uff1cApp \/\uff1e, document.getElementById(\u2019app\u2019));\n\nfunction menu(target, name) {\n    const originalRender = target.prototype.render;\n  target.prototype.render = function() {\n    return (\n            \uff1cdiv\uff1e\n                \uff1cul\uff1e\n                    \uff1cli\uff1e\u9805\u76ee1\uff1c\/li\uff1e\n                \uff1c\/ul\uff1e\n                { originalRender() }\n            \uff1c\/div\uff1e\n        );\n  };\n}\n<\/pre>\n<p>menu \u95a2\u6570\u307f\u3066\u3082\u3089\u3063\u305f\u3089\u3001\u3068\u3063\u3066\u3082\u30b7\u30f3\u30d7\u30eb\u306b\u4f5c\u308c\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3059\u306d\u3002<\/p>\n<p>\u5143\u3005\u306e render \u306e\u5185\u5bb9\u306f <code>target.prototype.render<\/code> \u306b\u5165\u3063\u3066\u304f\u308b\u306e\u3067\u3001<br \/>\n\u4e00\u65e6\u305d\u308c\u3092 <code>originalRender<\/code> \u5909\u6570\u306b\u5f85\u907f\u3055\u305b\u3001<br \/>\n<code>target.prototype.render<\/code> \u306b\u65b0\u3057\u304f\u63cf\u753b\u3059\u308b\u5185\u5bb9\u3092\u4e0a\u66f8\u304d\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u5143\u3005\u306e\u5185\u5bb9\u3092 <code>originalRender<\/code> \u306b\u5165\u308c\u3066\u3042\u308b\u306e\u3067\u3001<br \/>\nmenu \u3067\u5143\u306e\u5185\u5bb9\u3092\u63cf\u753b\u3055\u305b\u305f\u3044\u3068\u3053\u308d\u306b <code>{ originalRender() }<\/code> \u3092\u7f6e\u3044\u3066\u304a\u3051\u3070<br \/>\n\u305d\u3053\u306b\u5143\u3005\u306e\u5185\u5bb9\u304c\u5c55\u958b\u3055\u308c\u307e\u3059\u306d\u3002<\/p>\n<p>\u3068\u3063\u3066\u3082\u7c21\u5358\u3002<\/p>\n<h2>Functional Component \u306e\u5834\u5408\u3069\u3046\u3059\u308b\u306e\uff1f<\/h2>\n<p>\u5b9f\u306f\u30c7\u30b3\u30ec\u30fc\u30bf\u30fc\u3092\u4f7f\u3063\u305f Ver \u306b\u66f8\u304d\u76f4\u3057\u305f\u3068\u304d\u306b\u3001<br \/>\n\u3057\u308c\u3063\u3068\u5143\u306e\u65b9\u3092 class \u3092\u4f7f\u3063\u305f Component \u306b\u5dee\u3057\u66ff\u3048\u3066\u307e\u3057\u305f\u3002<\/p>\n<p>\u5143\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3069\u3061\u3089\u3082 Functional Component \u3092\u3064\u304b\u3063\u3066\u305f\u306f\u305a\u2026<\/p>\n<p>\u3067\u306f Functional Component \u306b\u3082\u5bfe\u5fdc\u3067\u304d\u308b\u3088\u3046\u306b\u30c7\u30b3\u30ec\u30fc\u30bf\u30fc\u3092\u66f8\u304d\u76f4\u305d\u3046\uff01\u3068\u306a\u308b\u306e\u3067\u3059\u304c\u3001<br \/>\n\u6b8b\u5ff5\u306a\u3053\u3068\u306b\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n@menu\nconst app = () =\uff1e (\n  \uff1ch1\uff1eApp\uff1c\/h1\uff1e\n);\n<\/pre>\n<p>\u307f\u305f\u3044\u306a\u3001\u5358\u7d14\u306a\u5909\u6570\u306b\u30c7\u30b3\u30ec\u30fc\u30bf\u30fc\u306f\u3064\u3051\u3089\u308c\u306a\u3044\u306e\u3067\u3059\u3002<br \/>\n\uff08\u30af\u30e9\u30b9\u306e\u4e2d\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u306a\u3089\u3067\u304d\u308b\u306e\u3067\u3059\u3051\u3069\u306d\u3002\uff09<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u3053\u3053\u3067 HOC (Higher-order Component) \u306b\u983c\u308a\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>HOC \u3067\u66f8\u304d\u76f4\u3059<\/h2>\n<p>\u3068\u306f\u3044\u3063\u3066\u3082\u3001\u7d50\u5c40\u3084\u308b\u3053\u3068\u306f\u307b\u307c\u4e00\u7dd2\u3067\u3001\u30af\u30e9\u30b9\u306e\u5834\u5408\u306f\u5148\u307b\u3069\u306e\u3088\u3046\u306b\u30c7\u30b3\u30ec\u30fc\u30bf\u30fc\u3082\u4f7f\u3048\u307e\u3059\u3002<br \/>\n\u5358\u7d14\u306b\u3001\u30c7\u30b3\u30ec\u30fc\u30bf\u30fc\u5185\u3067\u95a2\u6570\u3092\u5c55\u958b\u3057\u3066 render \u3092\u8d70\u3089\u305b\u3066\u3044\u305f\u3082\u306e\u3092\u3001<br \/>\n\u65b0\u3057\u3044 render \u3092\u542b\u3080 Component \u3092\u8fd4\u3059\u3088\u3046\u306b\u3057\u3066\u3042\u3052\u305f\u3089\u3044\u3044\u3060\u3051\u3067\u3059\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexport menu function (TargetComponent: any) {\n  return class Menu extends Component\uff1cany, any\uff1e {\n    render() {\n      return (\n        \uff1cdiv\uff1e\n          \uff1cMenu \/\uff1e\n          \uff1cTargetComponent {...this.props}\/\uff1e\n        \uff1c\/div\uff1e\n      );\n    }\n  }\n};\n<\/pre>\n<p>\u3053\u3093\u306a\u611f\u3058\u306b\u3002<\/p>\n<p>\u5b9f\u969b\u306b\u4f7f\u3046\u3068\u304d\u306f\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst dashboard: FC\uff1cHelloProps\uff1e = () =\uff1e (\n  \uff1ch1\uff1e\n    Hello!\n  \uff1c\/h1\uff1e\n);\n\nexport default menu(dashboard);\n<\/pre>\n<p>\u3053\u3093\u306a\u611f\u3058\u3067\u3001 Functional Component \u3092 HOC \u3067\u30e9\u30c3\u30d7\u3057\u3066\u3042\u3052\u305f\u3089 OK \u3067\u3059\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u5148\u307b\u3069\u540c\u69d8<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n@menu\nclass App extends React.Component {\n...\n<\/pre>\n<p>\u307f\u305f\u3044\u306a\u4f7f\u3044\u65b9\u3092\u3057\u3066\u3082\u3001\u540c\u3058\u3088\u3046\u306b\u52d5\u304f\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\uff08\u51e6\u7406\u3092\u8ffd\u3044\u304b\u3051\u3066\u307f\u305f\u3089\u3001\u3084\u3063\u3066\u3044\u308b\u3053\u3068\u306f\u540c\u3058\u3067\u3042\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3059\uff01\uff09<\/p>\n<h1>\u30ec\u30a4\u30a2\u30a6\u30c8\u306e\u3088\u3046\u306a\u3001\u4f7f\u3044\u56de\u305b\u308b\u90e8\u54c1\u306f HOC \u306b\u51fa\u3059\u3053\u3068\u3092\u691c\u8a0e\u3057\u3088\u3046\uff01<\/h1>\n<p>\u4ee5\u5916\u306b\u96e3\u3057\u305d\u3046\u3067\u3001\u5358\u7d14\u306b\u95a2\u6570\u3092\u30e9\u30c3\u30d4\u30f3\u30b0\u3057\u3066\u3042\u3052\u308b\u3060\u3051\u306a\u306e\u3067<br \/>\n\u5206\u304b\u3063\u3066\u3057\u307e\u3048\u3070\u7c21\u5358\u3067\u3059\u306d\u3002<\/p>\n<p>HOC \u306f\u3061\u3087\u3063\u3068\u624b\u3092\u51fa\u3057\u3065\u3089\u3044\u8a18\u6cd5\u3067\u3059\u304c\u3001\u4e00\u5ea6\u6163\u308c\u308b\u3068\u3068\u3066\u3082\u4fbf\u5229\u306a\u3082\u306e\u306a\u306e\u3067\u3001<br \/>\n\u662f\u975e\u3053\u3046\u3044\u3046\u7c21\u5358\u306a\u3068\u3053\u308d\u304b\u3089\u3064\u304b\u3063\u3066\u3044\u3063\u3066\u307f\u305f\u3044\u3067\u3059\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>React \u306e\u63cf\u753b\u5185\u5bb9\u3092 Decorator \u3067\u30e9\u30c3\u30d4\u30f3\u30b0\u3057\u3066\u307f\u308b\uff01 React \u66f8\u3044\u3066\u307e\u3059\u304b\uff1f \u6700\u8fd1\u79c1\u306f Vue \u306e\u65b9\u304c\u304a\u597d\u307f\u3067\u3059\u304c\u3001 \u3084\u3063\u3071\u308a\u8907\u96d1\u306a\u4e8b\u3092\u3057\u3088\u3046\u3068\u601d\u3046\u3068\u3001 JavaScript \u3092\u76f4\u63a5\u62e1\u5f35\u3057\u3066\u3044\u308b\u3088\u3046\u306a [&hellip;]<\/p>\n","protected":false},"author":15,"featured_media":15408,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10],"tags":[45,61],"class_list":["post-15405","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech","tag-javascript","tag-react-js"],"jetpack_featured_media_url":"https:\/\/www.agent-grow.com\/self20percent\/wp-content\/uploads\/2019\/10\/850c7612116999490846136e772ab7fd-1.png","jetpack_shortlink":"https:\/\/wp.me\/p7Bq4F-40t","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.agent-grow.com\/self20percent\/wp-json\/wp\/v2\/posts\/15405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.agent-grow.com\/self20percent\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.agent-grow.com\/self20percent\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.agent-grow.com\/self20percent\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.agent-grow.com\/self20percent\/wp-json\/wp\/v2\/comments?post=15405"}],"version-history":[{"count":0,"href":"https:\/\/www.agent-grow.com\/self20percent\/wp-json\/wp\/v2\/posts\/15405\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.agent-grow.com\/self20percent\/wp-json\/wp\/v2\/media\/15408"}],"wp:attachment":[{"href":"https:\/\/www.agent-grow.com\/self20percent\/wp-json\/wp\/v2\/media?parent=15405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.agent-grow.com\/self20percent\/wp-json\/wp\/v2\/categories?post=15405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.agent-grow.com\/self20percent\/wp-json\/wp\/v2\/tags?post=15405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}