{"id":105743,"date":"2017-03-30T20:04:36","date_gmt":"2017-03-30T11:04:36","guid":{"rendered":"http:\/\/www.key-p.com\/blog\/staff\/?p=105743"},"modified":"2017-09-01T18:46:04","modified_gmt":"2017-09-01T09:46:04","slug":"react%e3%81%a7document%e3%82%84window%e3%81%ae%e3%82%a4%e3%83%99%e3%83%b3%e3%83%88%e3%82%92%e8%a8%ad%e5%ae%9a%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.key-p.com\/blog\/staff\/archives\/105743","title":{"rendered":"React\u3067document\u3084window\u306e\u30a4\u30d9\u30f3\u30c8\u3092\u8a2d\u5b9a\u3059\u308b"},"content":{"rendered":"<p>\nt\u3067\u3059\u3002<br \/>\n\u4eca\u56de\u306f\u3001JavaScript\u306eView\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3042\u308b\u3001<a href=\"https:\/\/facebook.github.io\/react\/\" target=\"new\">React<\/a>\u306b\u3064\u3044\u3066\u66f8\u304d\u307e\u3059\u3002<br \/>\nReact\u3067\u3001JavaScript\u306edocument\u3084window\u306e\u30a4\u30d9\u30f3\u30c8\u3092\u6271\u3046\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8aac\u660e\u3057\u307e\u3059\u3002<br \/>\n<br \/>\nReact\u53ca\u3073ES2015\u4ee5\u964d\u306e\u69cb\u6587\u3092\u3042\u308b\u7a0b\u5ea6\u7406\u89e3\u3057\u3066\u3044\u308b\u524d\u63d0\u3067\u9032\u3081\u307e\u3059\u3002\u3054\u4e86\u627f\u304f\u3060\u3055\u3044\u3002<br \/>\n<br \/>\n<img loading=\"lazy\" class=\"aligncenter size-full wp-image-105758\" src=\"http:\/\/www.key-p.com\/blog\/staff\/wp-content\/uploads\/2017\/03\/icon_js.png\" alt=\"icon_js\" width=\"256\" height=\"256\" \/>\n<h3>React\u306e\u30a4\u30d9\u30f3\u30c8\u8a18\u8ff0\u306b\u3064\u3044\u3066<\/h3>\nReact\u3092\u5229\u7528\u3057\u3066\u30a4\u30d9\u30f3\u30c8\u3092\u8a18\u8ff0\u3059\u308b\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306bJSX\u8a18\u6cd5\u3067\u30a4\u30d9\u30f3\u30c8\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<br \/>\n<pre class=\"toolbar:1 lang:js decode:true\" title=\"example.jsx\">&lt;button onClick={() =&gt; console.log( 'clicked' )}&gt;\n  Click!\n&lt;\/button&gt;\n<\/pre>\n\u3057\u304b\u3057\u3001document\u3084window\u306f\u5143\u3005\u5b58\u5728\u3059\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306a\u306e\u3067\u3001\u5c11\u3057\u5de5\u592b\u3092\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n<h3>\u901a\u5e38\u306a\u3089...<\/h3>\n\u901a\u5e38\u3001document\u3084window\u306b\u30a4\u30d9\u30f3\u30c8\u3092\u5272\u308a\u5f53\u3066\u308b\u5834\u5408\u306f\u3001\u6b21\u306e\u3088\u3046\u306b\u884c\u3044\u307e\u3059\u3002<br \/>\n\u6b21\u306e\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3067\u3001\u30a6\u30a3\u30f3\u30c9\u30a6\u306e\u5927\u304d\u3055\u3092\u5909\u3048\u308b\u3068\u3001\u30b3\u30f3\u30bd\u30fc\u30eb\u306b\u30a4\u30d9\u30f3\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5185\u5bb9\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<br \/>\n<br \/>\n\u5225\u306e\u30ea\u30b9\u30ca\u30fc\u3092\u5165\u308c\u66ff\u3048\u3066\u5272\u308a\u5f53\u3066\u308b\u5834\u5408\u306f\u3001\u30a4\u30d9\u30f3\u30c8\u89e3\u9664\u3082\u8003\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n<pre class=\"toolbar:1 lang:js decode:true\" title=\"example.js\">\/\/ \u30ea\u30b9\u30ca\u30fc\u5b9a\u7fa9\nconst listener = (event) =&gt; console.log(event);\n\/\/ \u30a4\u30d9\u30f3\u30c8\u8a2d\u5b9a\nwindow.addEventListener('resize', listener);\n\/\/ \u30a4\u30d9\u30f3\u30c8\u89e3\u9664\nwindow.removeEventListener('resize', listener);\n<\/pre>\n<h3>JSX\u3067document\u3084window\u306b\u30a4\u30d9\u30f3\u30c8\u3092\u5272\u308a\u5f53\u3066\u308b\u3068...<\/h3>\nJSX\u8a18\u6cd5\u3067\u3001document\u3084window\u306e\u30a4\u30d9\u30f3\u30c8\u3092\u6271\u3048\u308b\u3088\u3046\u306b\u3057\u305f\u306e\u304c\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u3059\u3002<br \/>\n\u901a\u5e38\u306e\u5834\u5408\u3068\u540c\u3058\u3088\u3046\u306b\u3001\u30a6\u30a3\u30f3\u30c9\u30a6\u306e\u5927\u304d\u3055\u3092\u5909\u3048\u308b\u3068\u3001\u30b3\u30f3\u30bd\u30fc\u30eb\u306b\u30a4\u30d9\u30f3\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5185\u5bb9\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<br \/>\n<br \/>\n\u3053\u308c\u3060\u3068\u3001onResize\u3067\u5272\u308a\u5f53\u3066\u3066\u3044\u308b\u30ea\u30b9\u30ca\u30fc\u3092\u5165\u308c\u66ff\u3048\u3066\u3082\u3001\u81ea\u52d5\u3067\u5143\u306e\u30ea\u30b9\u30ca\u30fc\u306e\u30a4\u30d9\u30f3\u30c8\u89e3\u9664\u3092\u884c\u3044\u307e\u3059\u3002\u30a4\u30d9\u30f3\u30c8\u89e3\u9664\u3092\u8003\u3048\u308b\u5fc5\u8981\u304c\u306a\u304f\u306a\u308b\u308f\u3051\u3067\u3059\u3002<br \/>\n<pre class=\"toolbar:1 lang:js decode:true\" title=\"example.jsx\">&lt;EventListener\n  eventTarget={window}\n  onResize={(event) =&gt; console.log(event)}\n\/&gt;\n<\/pre>\nEventListener\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002<br \/>\n<pre class=\"toolbar:1 lang:js decode:true\" title=\"EventListener.js\">import React from 'react';\n\nexport default class EventListener extends React.Component {\n  static propTypes = {\n    eventTarget: React.PropTypes.object.isRequired,\n  };\n\n  componentDidMount() {\n    \/\/ \u30a4\u30d9\u30f3\u30c8\u306e\u8a2d\u5b9a\n    this.getListeners().forEach( ( [ type, listener ] ) =&gt; {\n      this.props.eventTarget.addEventListener( type, listener );\n    } );\n  }\n\n  componentWillUnmount() {\n    \/\/ \u30a4\u30d9\u30f3\u30c8\u306e\u8a2d\u5b9a\u89e3\u9664\n    this.getListeners().forEach( ( [ type, listener ] ) =&gt; {\n      this.props.eventTarget.removeEventListener( type, listener );\n    } );\n  }\n\n  getListeners() {\n    \/\/ props\u304b\u3089\u3001\u30a4\u30d9\u30f3\u30c8\u540d\u3068\u95a2\u6570\u3092\u62bd\u51fa\n    return Object\n      .keys( this.props )\n      .filter( key =&gt; key.startsWith( 'on' ) )\n      .map( key =&gt; [ key.slice( 2 ).toLowerCase(), this.props[key] ] );\n  }\n\n  render() {\n    return null; \/\/ \u4f55\u3082\u63cf\u753b\u3057\u306a\u3044\n  }\n}\n<\/pre>\n\u30cd\u30c3\u30c8\u3067\u8abf\u3079\u308b\u3068\u3001componentDidMount\u30fbcomponentWillUnmount\u3092\u5229\u7528\u3057\u3066\u3001\u30a4\u30d9\u30f3\u30c8\u306e\u767b\u9332\u30fb\u89e3\u9664\u3092\u884c\u3048\u3070\u826f\u3044\u3068\u3044\u3046\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3057\u305f\u3002<br \/>\n<br \/>\n\u6c4e\u7528\u7684\u306b\u4f7f\u3048\u308b\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306b\u3057\u305f\u304b\u3063\u305f\u306e\u3067\u3001props\u306b\u6e21\u3055\u308c\u305fon\u3067\u59cb\u307e\u308bkey\u306e\u5024\u3092\u3059\u3079\u3066\u30a4\u30d9\u30f3\u30c8\u3068\u3057\u3066\u6271\u3046\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002<br \/>\n\u306a\u306e\u3067\u3001<a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/EventTarget\" target=\"new\">EventTarget\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9<\/a>\u3092\u6301\u3064(addEventListener\u3068removeEventListener\u304c\u5229\u7528\u3067\u304d\u308b)\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3042\u308c\u3070\u3001EventListener\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u81ea\u4f53\u304c\u30a4\u30d9\u30f3\u30c8\u3092\u77e5\u3089\u306a\u304f\u3066\u3082\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n<h3>\u3061\u306a\u307f\u306b<\/h3>\nReact\u3068\u306f\u76f4\u63a5\u95a2\u4fc2\u306a\u3044\u3067\u3059\u304c\u3001\u4eca\u56de\u3001\u8a18\u4e8b\u306b\u3059\u308b\u306b\u3042\u305f\u3063\u3066\u3001EventListener\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3092Lodash\u3092\u4f7f\u308f\u306a\u3044\u30b3\u30fc\u30c9\u306b\u66f8\u304d\u76f4\u3057\u307e\u3057\u305f\u3002<br \/>\n\u6700\u8fd1\u3067\u306f\u3001<a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Object\/keys\" target=\"new\">Object.keys()<\/a>\u3084<a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/String\/startsWith\" target=\"new\">String.prototype.startsWith()<\/a>\u306a\u3069\u306e\u6a5f\u80fd\u304c\u6a19\u6e96\u3067\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u306a\u306e\u3067\u3001\u5c0f\u3055\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f5c\u6210\u3059\u308b\u7a0b\u5ea6\u3067\u3042\u308c\u3070\u3001\u4f9d\u5b58\u95a2\u4fc2\u3092\u6e1b\u3089\u3059\u305f\u3081\u306b\u3082\u3001Lodash\u306a\u3069\u3092\u4f7f\u308f\u306a\u304f\u3066\u3082\u826f\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<br \/>\n<br \/>\n<a href=\"http:\/\/www.key-p.com\/recruit\/\" target=\"new\">\u30ad\u30fc\u30fb\u30dd\u30a4\u30f3\u30c8\u3067\u306f\u3001\u3044\u308d\u3044\u308d\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3044\u3053\u306a\u3059\u30a8\u30f3\u30b8\u30cb\u30a2\u3092\u52df\u96c6\u3057\u3066\u3044\u307e\u3059\u3002<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>t\u3067\u3059\u3002 \u4eca\u56de\u306f\u3001JavaScript\u306eView\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3042\u308b\u3001React\u306b &hellip; <a href=\"https:\/\/www.key-p.com\/blog\/staff\/archives\/105743\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":40,"featured_media":105758,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.key-p.com\/blog\/staff\/wp-json\/wp\/v2\/posts\/105743"}],"collection":[{"href":"https:\/\/www.key-p.com\/blog\/staff\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.key-p.com\/blog\/staff\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.key-p.com\/blog\/staff\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.key-p.com\/blog\/staff\/wp-json\/wp\/v2\/comments?post=105743"}],"version-history":[{"count":25,"href":"https:\/\/www.key-p.com\/blog\/staff\/wp-json\/wp\/v2\/posts\/105743\/revisions"}],"predecessor-version":[{"id":106242,"href":"https:\/\/www.key-p.com\/blog\/staff\/wp-json\/wp\/v2\/posts\/105743\/revisions\/106242"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.key-p.com\/blog\/staff\/wp-json\/wp\/v2\/media\/105758"}],"wp:attachment":[{"href":"https:\/\/www.key-p.com\/blog\/staff\/wp-json\/wp\/v2\/media?parent=105743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.key-p.com\/blog\/staff\/wp-json\/wp\/v2\/categories?post=105743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.key-p.com\/blog\/staff\/wp-json\/wp\/v2\/tags?post=105743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}