0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Reactでjsonファイルを読み込む

Posted at

はじめに

Reactを使っている際に、動作テスト用にローカル配下のjsonデータを読み込むことがあったので、やり方を備忘録程度に書き残す。
(他にやり方あれば是非コメント欄に)

やり方

方法

import文を使って読み込む

import Data from "[jsonファイルのパス]"

カスタムフックでの利用を想定した場合のものを以下に記載する。

useAllUser.ts
import { useState } from "react"
import { User } from "../types/api/User"
import Data from "../_test_data/user.json"; // これを追加する

export const useAllUser = () => {
    const [users, setUsers] = useState<Array<User>>([])
    const getUsers = () => setUsers(Data)
    return { users, getUsers }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?