{"id":623,"date":"2021-07-26T13:03:59","date_gmt":"2021-07-26T04:03:59","guid":{"rendered":"https:\/\/weseek.co.jp\/tech\/?p=623"},"modified":"2023-04-17T10:28:55","modified_gmt":"2023-04-17T01:28:55","slug":"%e3%80%90react-hooks%e3%80%91%e4%bb%8a%e6%97%a5%e3%81%8b%e3%82%89%e4%bd%bf%e3%81%88%e3%82%8b-useeffect-%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6%e5%9f%ba%e7%a4%8e%e3%81%8b%e3%82%89%e7%90%86%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/weseek.co.jp\/tech\/623\/","title":{"rendered":"ReactHooks|\u4eca\u65e5\u304b\u3089\u4f7f\u3048\u308buseEffect"},"content":{"rendered":"<p>\u3053\u306e\u6295\u7a3f\u306f\u3001\u5f0a\u793e\u304c\u63d0\u4f9b\u3059\u308b<strong>WESEEK TECH\u901a\u4fe1<\/strong>\u306e\u4e00\u74b0\u3067\u3059\u3002<br \/>\nWESEEK TECH\u901a\u4fe1\u3068\u306f\u3001WESEEK\u306e\u30a8\u30f3\u30b8\u30cb\u30a2\u304c\u30ad\u30e3\u30c3\u30c1\u30a2\u30c3\u30d7\u3057\u305f\u6280\u8853\u306b\u95a2\u3059\u308b\u60c5\u5831\u3092\u3001tech\u30d6\u30ed\u30b0\u3092\u901a\u3058\u3066\u5b9a\u671f\u7684\u306b\u767a\u4fe1\u3057\u3066\u3044\u304f\u3082\u306e\u3067\u3059\u3002<\/p>\n<h2>\u306f\u3058\u3081\u306b<\/h2>\n<p>\u306f\u3058\u3081\u307e\u3057\u3066\u3001\u30b7\u30b9\u30c6\u30e0\u30a8\u30f3\u30b8\u30cb\u30a2\u306e <a href=\"https:\/\/github.com\/kaoritokashiki\">\u304b\u304a\u308a<\/a> \u3067\u3059\u3002\u682a\u5f0f\u4f1a\u793e WESEEK\u3067\u30a4\u30f3\u30bf\u30fc\u30f3\u3092\u7d4c\u3066\u65b0\u5352\u3067\u5165\u793e\u3001\u666e\u6bb5\u306e\u696d\u52d9\u3067\u306f OSS\u3067\u3042\u308b <a href=\"https:\/\/growi.org\/ja\/\">GROWI<\/a> \u306e\u958b\u767a\u3092\u884c\u3063\u3066\u3044\u307e\u3059\u3002\u3069\u3046\u305e\u3088\u308d\u3057\u304f\u304a\u9858\u3044\u3044\u305f\u3057\u307e\u3059\u3002<\/p>\n<p>\u8a18\u5ff5\u3059\u3079\u304d\u521d\u7a3f\u306f\u3001React Hooks\u306e\u4e2d\u3067\u3082\u4e3b\u8981\u3067\u4f7f\u7528\u983b\u5ea6\u306e\u9ad8\u3044 <code>useEffect<\/code>\u306b\u3064\u3044\u3066\u307e\u3068\u3081\u3066\u307f\u307e\u3057\u305f\uff01<\/p>\n<p><!--more--><\/p>\n\n<h2>useEffect \u3068\u306f<\/h2>\n<ul>\n<li>\u30ec\u30f3\u30c0\u30fc\u306e\u7d50\u679c\u304c\u753b\u9762\u306b\u53cd\u6620\u3055\u308c\u305f\u5f8c\u306b\u5b9f\u884c\u3055\u308c\u308b\u95a2\u6570\u3067\u3059\u3002<\/li>\n<li>\u30af\u30e9\u30b9\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306b\u304a\u3051\u308b\u30e9\u30a4\u30d5\u30b5\u30a4\u30af\u30eb\u30e1\u30bd\u30c3\u30c9\u306b\u3042\u305f\u308a\u307e\u3059\u3002<br \/>\ncomponentDidMount \/ componentDidUpdate \/ componentWillUnmount<\/li>\n<li>\u95a2\u6570\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u5185\u3067\u3001\u526f\u4f5c\u7528(DOM\u306e\u66f8\u304d\u63db\u3048\u3001\u5909\u6570\u4ee3\u5165\u3001API\u901a\u4fe1\u306a\u3069\u306e\u51e6\u7406\u306a\u3069)\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059<\/li>\n<\/ul>\n<p>\u526f\u4f5c\u7528\u306b\u95a2\u3057\u3066\u308f\u304b\u308a\u3065\u3089\u3044\u5834\u5408\u306f\u3001<a href=\"https:\/\/qiita.com\/Mitsuw0\/items\/801f783ca74b062c1ed8\">React\u306b\u304a\u3051\u308b\u300c\u526f\u4f5c\u7528\u300d\u3068\u306f\uff1f<\/a>\u3092\u4e00\u8aad\u3057\u3066\u304a\u304f\u3068\u826f\u3044\u3067\u3057\u3087\u3046<\/p>\n<p><img src=\"https:\/\/storage.googleapis.com\/zenn-user-upload\/fde472e1d52fe823f8340c2a.png\" alt=\"\" \/><\/p>\n<pre><code>import React, {useState, useEffect} from &#039;react&#039;;\n\nconst UseEffectExample = () =&gt; {\n  const [count, setCount] = useState(0);\n\n  return (\n    &lt;div&gt;\n      &lt;p&gt;The current count is {count}&lt;\/p&gt;\n      &lt;button onClick={() =&gt; setCount(count + 1 )}&gt;+1&lt;\/button&gt;\n      &lt;button onClick={() =&gt; setCount(0)}&gt;reset&lt;\/button&gt;\n      &lt;button onClick={() =&gt; setCount(count + 1 )}&gt;+1&lt;\/button&gt;\n    &lt;\/div&gt;\n  )\n}\n\nexport default UseEffectExample;<\/code><\/pre>\n<p>useState\u3092\u4f7f\u3063\u3066count\u306estate\u5909\u6570\u3092\u8a2d\u5b9a\u3057\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u5ea6\u306bCount\u6570\u304c\u5897\u3048\u308b\u30b3\u30fc\u30c9\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\n\u5909\u6570count\u306b\u306f\u521d\u671f\u5024\u30670\u304c\u6307\u5b9a\u3055\u308c\u3066\u304a\u308a\u3001-1\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u3068\u30de\u30a4\u30ca\u30b91\u3001+1\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u3068\u30d7\u30e9\u30b91\u3065\u3064\u5897\u3048\u3066\u3044\u304f\u3002\u307e\u305f\u3001reset\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u30680\u306b\u623b\u308b\u3068\u3044\u3046\u4ed5\u7d44\u307f\u306e\u30ab\u30a6\u30f3\u30c8\u6a5f\u80fd\u3092\u5143\u306b\u8aac\u660e\u3057\u307e\u3059\u3002<\/p>\n<h3>1. \u7b2c\u4e8c\u5f15\u6570\u306b\u4f55\u3082\u6e21\u3055\u306a\u3044\u5834\u5408<\/h3>\n<pre><code>  useEffect(() => {\n     console.log('useEffect ran');\n  })<\/code><\/pre>\n<p>\u3053\u306e\u5834\u5408\u3001\u300c\u6bce\u56de\u306e\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u5f8c\u300d\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<h3>2. \u7b2c\u4e8c\u5f15\u6570\u306b\u7a7a\u306e\u914d\u5217\u3092\u6e21\u3059\u5834\u5408<\/h3>\n<pre><code>  useEffect(()=>{\n    console.log('This should only run once')\n  }, [])<\/code><\/pre>\n<p>\u7b2c\u4e8c\u5f15\u6570\u306b\u7a7a\u306e\u914d\u5217\u3092\u8a18\u8f09\u3057\u305f\u5834\u5408\u3001\u300c\u4e00\u756a\u6700\u521d\u306e\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u5f8c\u300d\u306b\u306e\u307f\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<h3>3. \u7b2c\u4e8c\u5f15\u6570\u306b\u4f55\u3089\u304b\u306e\u5024(\u5909\u6570\u7b49)\u3092\u6e21\u3059\u5834\u5408<\/h3>\n<pre><code>  useEffect(() => {\n     console.log('useEffect ran');\n  },[count])<\/code><\/pre>\n<p>\u3053\u306e\u5834\u5408\u3001\u300c\u4e00\u756a\u6700\u521d\u306e\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u5f8c\u300d\u3068\u300c\u7b2c\u4e8c\u5f15\u6570\u306b\u6307\u5b9a\u3057\u305fstate\u3082\u3057\u304f\u306fprops\u306e\u5024\u304c\u5909\u66f4\u3057\u305f\u6642\u300d\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002\u4eca\u56de\u306e\u4f8b\u3067\u3044\u3046\u3068\u3001<strong>\u6700\u521d\u306e\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u5f8c\u306b\u52a0\u3048\u3001count\u306e\u5024\u306b\u5909\u66f4\u304c\u751f\u3058\u305f\u6642\u306b\u5b9f\u884c<\/strong> \u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u7b2c\u4e8c\u5f15\u6570\u306e\u914d\u5217\u306b\u5177\u4f53\u7684\u306a\u5024\u3092\u6307\u5b9a\u3057\u3066\u3042\u3052\u308b\u3053\u3068\u3067\u3001\u5b9f\u884c\u3055\u308c\u308b\u30bf\u30a4\u30df\u30f3\u30b0\u3092\u9650\u5b9a\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u3063\u3066\u3001\u4e0d\u8981\u306a\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u304c\u6e1b\u308b\u306e\u3067\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u6539\u5584\u306b\u3064\u306a\u304c\u308a\u307e\u3059\uff01<\/p>\n<h3>\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\u95a2\u6570<\/h3>\n<p>\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\u95a2\u6570\u3068\u306faddEventLitener\u306e\u524a\u9664\u3001\u30bf\u30a4\u30de\u30fc\u306e\u30ad\u30e3\u30f3\u30bb\u30eb\u306a\u3069\u306b\u4f7f\u308f\u308c\u307e\u3059\u3002<\/p>\n<p>useEffect\u3067\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\u95a2\u6570\u3092return\u3059\u308b\u3053\u3068\u3067\u3001\uff12\u5ea6\u76ee\u4ee5\u964d\u306e\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u6642\u306b\u524d\u56de\u306e\u526f\u4f5c\u7528\u3092\u6d88\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code>useEffect(\n  () => {\n    const subscription = props.source.subscribe();\n    return () => {\n      subscription.unsubscribe();\n    };\n  },\n  [props.source],\n);<\/code><\/pre>\n<p>(\u203b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u3001<a href=\"https:\/\/ja.reactjs.org\/docs\/hooks-reference.html#conditionally-firing-an-effect\">React\u3000\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/a>\u304b\u3089\u629c\u7c8b\u3057\u3066\u3044\u307e\u3059)<\/p>\n<h2>useEffect\u3000\u3092\u4f7f\u7528\u3059\u308b\u969b\u306eTips\uff01<\/h2>\n<p>\u30af\u30e9\u30b9\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u30e9\u30a4\u30d5\u30b5\u30a4\u30af\u30eb\u3067\u306f\u3001\u30e9\u30a4\u30d5\u30b5\u30a4\u30af\u30eb\u30e1\u30bd\u30c3\u30c9\u306b\u95a2\u4fc2\u306e\u306a\u3044\u30ed\u30b8\u30c3\u30af\u304c\u542b\u307e\u308c\u3066\u3057\u307e\u3063\u305f\u308a\u3001\u95a2\u9023\u306e\u3042\u308b\u30ed\u30b8\u30c3\u30af\u304c\u8907\u6570\u306e\u30e1\u30bd\u30c3\u30c9\u306b\u5206\u96e2\u3057\u3066\u3057\u307e\u3063\u305f\u308a\u3059\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n\u4ee5\u4e0b\u306e\u4f8b\u3067\u306f\u3001<code>document.title<\/code>\u304c ComponentDidMount \u3068 ComponentDidUpdate\u306b\u5206\u96e2\u3057\u3066\u3057\u307e\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre><code>class FriendStatusWithCounter extends React.Component {\n  constructor(props) {\n    super(props);\n    this.state = { count: 0, isOnline: null };\n    this.handleStatusChange = this.handleStatusChange.bind(this);\n  }\n\n  componentDidMount() {\n    document.title = <code>You clicked ${this.state.count} times<\/code>;\n    ChatAPI.subscribeToFriendStatus(\n      this.props.friend.id,\n      this.handleStatusChange\n    );\n  }\n\n  componentDidUpdate() {\n    document.title = <code>You clicked ${this.state.count} times<\/code>;\n  }\n\n  componentWillUnmount() {\n    ChatAPI.unsubscribeFromFriendStatus(\n      this.props.friend.id,\n      this.handleStatusChange\n    );\n  }\n\n  handleStatusChange(status) {\n    this.setState({\n      isOnline: status.isOnline\n    });\n  }\n  \/\/ ...<\/code><\/pre>\n<p>\u4e0a\u8a18\u3067\u8ff0\u3079\u305f\u554f\u984c\u70b9\u306f\u3001\u30d5\u30c3\u30af\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code>function FriendStatusWithCounter(props) {\n  const [count, setCount] = useState(0);\n  useEffect(() => {\n    document.title = <code>You clicked ${count} times<\/code>;\n  });\n\n  const [isOnline, setIsOnline] = useState(null);\n  useEffect(() => {\n    function handleStatusChange(status) {\n      setIsOnline(status.isOnline);\n    }\n\n    ChatAPI.subscribeToFriendStatus(props.friend.id, handleStatusChange);\n    return () => {\n      ChatAPI.unsubscribeFromFriendStatus(props.friend.id, handleStatusChange);\n    };\n  });\n  \/\/ ...\n}<\/code><\/pre>\n<p><strong>\u30e9\u30a4\u30d5\u30b5\u30a4\u30af\u30eb\u3067\u306f\u306a\u304f\u3001\u4f55\u3092\u884c\u3046\u306e\u304b\u306b\u3088\u3063\u3066\u30b3\u30fc\u30c9\u3092\u5206\u5272<\/strong> \u3067\u304d\u307e\u3059\u3002<br \/>\n\u95a2\u9023\u306e\u3042\u308b\u30ed\u30b8\u30c3\u30af\u3067\u51e6\u7406\u3092\u5206\u3051\u308b\u306e\u3067\u3001\u308f\u304b\u308a\u3084\u3059\u3044\u3060\u3051\u3067\u306a\u304f\u7121\u99c4\u306a\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3092\u6e1b\u3089\u3059\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>(\u203b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f<a href=\"https:\/\/ja.reactjs.org\/docs\/hooks-effect.html#tip-use-multiple-effects-to-separate-concerns\">React \u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/a>\u304b\u3089\u629c\u7c8b\u3057\u3066\u3044\u307e\u3059)<\/p>\n<h2>\u304a\u308f\u308a\u306b<\/h2>\n<p>useEffect\u306fReact Hooks\u306e\u4e2d\u3067\u3082\u7279\u306b\u4f7f\u3046\u30b7\u30fc\u30f3\u304c\u591a\u3044\u306e\u3067\u3001\u3057\u3063\u304b\u308a\u3068\u52d5\u4f5c\u3092\u7406\u89e3\u3057\u3066\u4f7f\u7528\u3057\u307e\u3057\u3087\u3046\uff01<\/p>\n<h2>\u53c2\u8003\u306b\u3055\u305b\u3066\u3044\u305f\u3060\u3044\u305f\u30b5\u30a4\u30c8<\/h2>\n<ul>\n<li>\n<p>React \u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/p>\n<ul>\n<li><a href=\"https:\/\/reactjs.org\/docs\/hooks-effect.html\">Using the Effect Hook<\/a><\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Stack overflow<\/p>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/54002792\/should-i-use-one-or-many-useeffect-in-component\">Should I use one or many useEffect in component?<\/a><br \/>\n<h2>\u95a2\u9023\u8a18\u4e8b<\/h2>\n<p><a href=\"https:\/\/weseek.co.jp\/tech\/837\/\">Rails\u00d7React\u00d7TypeScript\u3067\u4f5c\u308bWEB\u30a2\u30d7\u30ea\u5165\u9580<\/a><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u306e\u6295\u7a3f\u306f\u3001\u5f0a\u793e\u304c\u63d0\u4f9b\u3059\u308bWESEEK TECH\u901a\u4fe1\u306e\u4e00\u74b0\u3067\u3059\u3002 WESEEK TECH\u901a\u4fe1\u3068\u306f\u3001WESEEK\u306e\u30a8\u30f3\u30b8\u30cb\u30a2\u304c\u30ad\u30e3\u30c3\u30c1\u30a2\u30c3\u30d7\u3057\u305f\u6280\u8853\u306b\u95a2\u3059\u308b\u60c5\u5831\u3092\u3001tech\u30d6\u30ed\u30b0\u3092\u901a\u3058\u3066\u5b9a\u671f\u7684\u306b\u767a\u4fe1\u3057\u3066\u3044\u304f\u3082\u306e\u3067\u3059\u3002 \u306f\u3058<\/p>\n","protected":false},"author":3,"featured_media":647,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[51],"tags":[],"_links":{"self":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/623"}],"collection":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/comments?post=623"}],"version-history":[{"count":15,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/623\/revisions"}],"predecessor-version":[{"id":4049,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/623\/revisions\/4049"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/media\/647"}],"wp:attachment":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/media?parent=623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/categories?post=623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/tags?post=623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}