# カスタムフックス

**カスタムフックス**とは、コンポーネントのロジック部分を切り出し関数化する手法です。**カスタムフックス**を使うことで、ロジックを分けることができ、同じロジックを別のところで使い回すことができます。

`src`配下に`hooks`ディレクトリを作成しましょう。その中に`usePostList.ts`を作成しましょう。カスタムフックスの命名は基本的に`use~`としましょう。`usePostList.ts`内には`usePostList`という関数を定義しましょう。

<pre class="language-typescript" data-title="usePostList.ts◎"><code class="lang-typescript"><strong>export const usePostList = () => {}
</strong></code></pre>

この関数の中に、`PostList.tsx`のロジック部分を移行しましょう。

<pre class="language-tsx" data-title="usePostList.ts◎"><code class="lang-tsx"><strong>import axios from "axios";
</strong><strong>import { useEffect, useState } from "react";
</strong>
<strong>type Post = {
</strong><strong>  id: number;
</strong><strong>  userId: number;
</strong><strong>  title: string;
</strong><strong>  body: string;
</strong><strong>};
</strong>
export const usePostList = () => {
<strong>  const [postList, setPostList] = useState&#x3C;Post[]>([]);
</strong><strong>  const [userId, setUserId] = useState(1);
</strong><strong>  const [errorMessage, setErrorMessage] = useState("");
</strong>
<strong>  useEffect(() => {
</strong><strong>    axios
</strong><strong>      .get&#x3C;Post[]>("https://jsonplaceholder.typicode.com/posts")
</strong><strong>      .then((res) => {
</strong><strong>        setPostList(res.data);
</strong><strong>      })
</strong><strong>      .catch((e) => {
</strong><strong>        setErrorMessage(e.message);
</strong><strong>      });
</strong><strong>  }, []);
</strong>};
</code></pre>

そして、この関数の返却値として、`PostList.tsx`で使用するstateや関数を返却するようにします。

<pre class="language-typescript" data-title="usePostList.ts◎"><code class="lang-typescript">... 
  }, []);

<strong>  return {
</strong><strong>    postList,
</strong><strong>    userId,
</strong><strong>    errorMessage,
</strong><strong>    setUserId,
</strong><strong>  };
</strong>};

</code></pre>

上記にて作成したフックスを呼び出します。上記を呼び出すには、`const { ... } = usePostList()`と呼び出すことで、stateや関数を取得することができます。

ですので、`PostList.tsx`のロジック部分と不要なインポートを削除して、`usePostList()`を呼び出します。最終的には以下のような形になります。

<pre class="language-tsx" data-title="PostList.tsx◎"><code class="lang-tsx"><strong>import { usePostList } from "../hooks/usePostList";
</strong>
export const PostList = () => {
<strong>  const { postList, userId, errorMessage, setUserId } = usePostList();
</strong>
  return (
    &#x3C;div>
      &#x3C;h2>PostList&#x3C;/h2>
      &#x3C;div>
        &#x3C;p>userID: {userId}&#x3C;/p>
        &#x3C;input
          type="number"
          value={userId}
          onChange={(e) => setUserId(Number(e.target.value))}
        />
      &#x3C;/div>
      {errorMessage !== "" &#x26;&#x26; (
        &#x3C;div className="alert alert-danger" role="alert">
          {errorMessage}
        &#x3C;/div>
      )}
      &#x3C;table className="table">
        &#x3C;thead>
          &#x3C;tr>
            &#x3C;th scope="col">id&#x3C;/th>
            &#x3C;th scope="col">ユーザーid&#x3C;/th>
            &#x3C;th scope="col">タイトル&#x3C;/th>
            &#x3C;th scope="col">内容&#x3C;/th>
          &#x3C;/tr>
        &#x3C;/thead>
        &#x3C;tbody>
          {postList
            .filter((post) => post.userId === userId)
            .map((post) => (
              &#x3C;tr key={post.id}>
                &#x3C;td>{post.id}&#x3C;/td>
                &#x3C;td>{post.userId}&#x3C;/td>
                &#x3C;td>{post.title}&#x3C;/td>
                &#x3C;td>{post.body}&#x3C;/td>
              &#x3C;/tr>
            ))}
        &#x3C;/tbody>
      &#x3C;/table>
    &#x3C;/div>
  );
};
</code></pre>

このようにロジックを別ファイルに切り出すことで、コンポーネントがスッキリするかと思います。
