質問編集履歴

2

APIキー載せてたのでダミーにしました

2020/08/03 04:06

投稿

DaisukeMori
DaisukeMori

スコア226

test CHANGED
File without changes
test CHANGED
@@ -60,7 +60,7 @@
60
60
 
61
61
 
62
62
 
63
- const keyId = '28c2d30bb92248c4aedec33bcc623a0d'; // ぐるなびAPI KEY
63
+ const keyId = 'your_key'; // ぐるなびAPI KEY
64
64
 
65
65
  const pref = 'PREF28'; // 現在こちらを直書きでエリア指定しています
66
66
 

1

もっと具体的に修正

2020/08/03 04:06

投稿

DaisukeMori
DaisukeMori

スコア226

test CHANGED
File without changes
test CHANGED
@@ -29,3 +29,121 @@
29
29
  const id = propid;
30
30
 
31
31
  ```
32
+
33
+
34
+
35
+
36
+
37
+ # 追記
38
+
39
+
40
+
41
+ 下記コードは`const pref = 'PREF28';`の部分で兵庫県エリアを直書きで指定しています。
42
+
43
+ こちらを親から`~/:id`で渡したパラメータを変数として入れたい。
44
+
45
+ ただHyogo関数内にしかpropsで受け渡したデータを使用することができません。
46
+
47
+ また試しに`const keyId`〜検索クエリ一式をHyogo関数内に入れるとうまくfetchしてくれません。
48
+
49
+
50
+
51
+ なのでやったこととしては、`const keyId`の前で新たにpropsを呼び出す関数をつくればいけるかと思ったのですが、
52
+
53
+ 回答のコードで試してもundefiedで値が取得できませんでした。
54
+
55
+
56
+
57
+ ```jsx
58
+
59
+ // import省略
60
+
61
+
62
+
63
+ const keyId = '28c2d30bb92248c4aedec33bcc623a0d'; // ぐるなびAPI KEY
64
+
65
+ const pref = 'PREF28'; // 現在こちらを直書きでエリア指定しています
66
+
67
+ const hitPerPage = 99; // 件数(Max100)
68
+
69
+ const lunch = 0; // 0 or 1
70
+
71
+ const takeout = 1; // 0 or 1
72
+
73
+ const bento = 1; // 0 or 1
74
+
75
+ const deliverly = 0; // 0 or 1
76
+
77
+
78
+
79
+ // 検索クエリ
80
+
81
+ const URL = `https://api.gnavi.co.jp/RestSearchAPI/v3/?
82
+
83
+ keyid=${keyId}&
84
+
85
+ pref=${pref}&
86
+
87
+ hit_per_page=${hitPerPage}&
88
+
89
+ lunch=${lunch}&
90
+
91
+ takeout=${takeout}&
92
+
93
+ bento=${bento}&
94
+
95
+ deliverly=${deliverly}`;
96
+
97
+
98
+
99
+ const Hyogo = (props) => {
100
+
101
+ const [articles, setArticles] = useState([]);
102
+
103
+
104
+
105
+ useEffect(() => {
106
+
107
+ fetchArticles();
108
+
109
+ }, []);
110
+
111
+
112
+
113
+ const fetchArticles = async () => {
114
+
115
+ try {
116
+
117
+ const res = await axios.get(URL);
118
+
119
+ setArticles(res.data.rest);
120
+
121
+ } catch (error) {
122
+
123
+ console.error(error);
124
+
125
+ }
126
+
127
+ }
128
+
129
+
130
+
131
+ return (
132
+
133
+ <div className="article">
134
+
135
+ {/* 省略 */}
136
+
137
+ </div>
138
+
139
+ );
140
+
141
+ }
142
+
143
+
144
+
145
+ export default Hyogo;
146
+
147
+
148
+
149
+ ```