質問編集履歴

2

文字修正

2020/05/19 07:34

投稿

kaoru_tujimiya
kaoru_tujimiya

スコア36

test CHANGED
File without changes
test CHANGED
@@ -10,508 +10,508 @@
10
10
 
11
11
  document.getElementById("users").textContent = users;
12
12
 
13
+ const pageviews = res.result.reports[0].data.totals[0].values[1];
14
+
15
+ document.getElementById("pageviews").textContent = pageviews;
16
+
17
+ ```
18
+
19
+ をIDベースで、
20
+
21
+ returnして、
22
+
23
+ ```react
24
+
25
+ <div className={access}>
26
+
27
+ <dl>
28
+
29
+ <dt>
30
+
31
+ <span>サイト訪問数</span>
32
+
33
+ </dt>
34
+
35
+ <dd id="users"></dd>
36
+
37
+ </dl>
38
+
39
+ </div>
40
+
41
+ <div className={access}>
42
+
43
+ <dl>
44
+
45
+ <dt>
46
+
47
+ <span>ページ閲覧数</span>
48
+
49
+ </dt>
50
+
51
+ <dd id="pageviews"></dd>
52
+
53
+ </dl>
54
+
55
+ </div>
56
+
57
+ ```
58
+
59
+ として表示しているのですが、
60
+
61
+ これを仮想DOMで使いたい場合どういう実装をすればよろしいでしょうか?
62
+
63
+ お手数ですが、よろしくお願いいたします、
64
+
65
+
66
+
67
+
68
+
69
+ ### 発生している問題・エラーメッセージ
70
+
71
+
72
+
73
+ ```
74
+
75
+ エラーメッセージ
76
+
77
+ ```
78
+
79
+
80
+
81
+ ### 該当のソースコード
82
+
83
+
84
+
85
+ ```react
86
+
87
+ import React, { useState, useEffect } from "react";
88
+
89
+ import { Spin, Button, Icon, DatePicker } from "antd";
90
+
91
+ import moment from "moment";
92
+
93
+
94
+
95
+ const { gapi } = window;
96
+
97
+ const { RangePicker } = DatePicker;
98
+
99
+ const dateFormat = "YYYY-MM-DD";
100
+
101
+
102
+
103
+ const Aaa = () => {
104
+
105
+ const [state, setState] = useState({
106
+
107
+ viewId: "",
108
+
109
+ clientId: "",
110
+
111
+ startDate: "2222-22-01",
112
+
113
+ endDate: "2222-22-01",
114
+
115
+ users: "ga:users",
116
+
117
+ pageviews: "ga:pageviews",
118
+
119
+ });
120
+
121
+ const [signedIn, setSignedIn] = useState(false);
122
+
123
+
124
+
125
+ useEffect(() => {
126
+
127
+ if (!settingData) return;
128
+
129
+ const { gaConfig } = JSON.parse(settingData.setting.values);
130
+
131
+ const { viewId, clientId } = JSON.parse(gaConfig);
132
+
133
+ const f = async () => {
134
+
135
+ setState({
136
+
137
+ ...state,
138
+
139
+ viewId,
140
+
141
+ clientId,
142
+
143
+ });
144
+
145
+ };
146
+
147
+ f();
148
+
149
+ }, [settingData]);
150
+
151
+
152
+
153
+ useEffect(() => {
154
+
155
+ if (!state.clientId) return;
156
+
157
+ const f = async () => {
158
+
159
+ console.log(state.clientId);
160
+
161
+ gapiLoad();
162
+
163
+ };
164
+
165
+ f();
166
+
167
+ // eslint-disable-next-line react-hooks/exhaustive-deps
168
+
169
+ }, [state.clientId]);
170
+
171
+
172
+
173
+ // Query the API and print the results to the page.
174
+
175
+ useEffect(() => {
176
+
177
+ if (!signedIn) return;
178
+
179
+ const f = async () => {
180
+
181
+ await sendQuery();
182
+
183
+ };
184
+
185
+ f();
186
+
187
+ // eslint-disable-next-line react-hooks/exhaustive-deps
188
+
189
+ }, [signedIn, state.startDate, state.endDate]);
190
+
191
+
192
+
193
+ if (settingError) {
194
+
195
+ const error = settingError;
196
+
197
+ return `Error! ${error}`;
198
+
199
+ }
200
+
201
+
202
+
203
+ const sendQuery = async () => {
204
+
205
+ const arg = {
206
+
207
+ path: "/v4/reports:batchGet",
208
+
209
+ root: "https://analyticsreporting.googleapis.com/",
210
+
211
+ method: "POST",
212
+
213
+ body: {
214
+
215
+ reportRequests: [
216
+
217
+ {
218
+
219
+ viewId: state.viewId,
220
+
221
+ dateRanges: [
222
+
223
+ {
224
+
225
+ startDate: state.startDate,
226
+
227
+ endDate: state.endDate,
228
+
229
+ },
230
+
231
+ ],
232
+
233
+ metrics: [
234
+
235
+ {
236
+
237
+ expression: state.users,
238
+
239
+ },
240
+
241
+ {
242
+
243
+ expression: state.pageviews,
244
+
245
+ },
246
+
247
+ ],
248
+
249
+ },
250
+
251
+ ],
252
+
253
+ },
254
+
255
+ };
256
+
257
+
258
+
259
+ const res = await gapiRequest(arg).catch((err) => err);
260
+
261
+ // invalid viewId
262
+
263
+ if (res.result.error) {
264
+
265
+ console.log(res.result.error);
266
+
267
+ return;
268
+
269
+ }
270
+
271
+ // const formattedJson = JSON.stringify(res.result.reports[0], null, 2);
272
+
273
+ const users = res.result.reports[0].data.totals[0].values[0];
274
+
275
+ document.getElementById("users").textContent = users;
276
+
13
277
  const pageviews = res.result.reports[0].data.totals[0].values[1];
14
278
 
15
279
  document.getElementById("pageviews").textContent = pageviews;
16
280
 
281
+ };
282
+
283
+
284
+
285
+ const gapiLoad = async () => {
286
+
287
+ gapi.load("auth2", async (signin2) => {
288
+
289
+ gapi.signin2.render("google-signin-button", {
290
+
291
+ onsuccess: (res) => {
292
+
293
+ console.log(res);
294
+
295
+ setSignedIn(true);
296
+
297
+ },
298
+
299
+ onfailure: (res) => {
300
+
301
+ console.log(res.error);
302
+
303
+ // setSignedIn(true)
304
+
305
+ },
306
+
307
+ });
308
+
309
+ const res = await gapi.auth2
310
+
311
+ .init({
312
+
313
+ client_id: state.clientId,
314
+
315
+ scope: "https://www.googleapis.com/auth/analytics.readonly",
316
+
317
+ })
318
+
319
+ .then((res) => res)
320
+
321
+ .catch((err) => err);
322
+
323
+ if (res.error) {
324
+
325
+ console.log(res.error);
326
+
327
+ return;
328
+
329
+ }
330
+
331
+ });
332
+
333
+ };
334
+
335
+
336
+
337
+ const onSignIn = async () => {
338
+
339
+ const auth = gapi.auth2.getAuthInstance();
340
+
341
+ const res = await auth.signIn().catch((err) => err);
342
+
343
+ if (res.error) {
344
+
345
+ console.log(res.error);
346
+
347
+ return;
348
+
349
+ }
350
+
351
+ setSignedIn(true);
352
+
353
+ };
354
+
355
+
356
+
357
+ const onSignOut = () => {
358
+
359
+ const auth = gapi.auth2.getAuthInstance();
360
+
361
+ auth.signOut();
362
+
363
+ setSignedIn(false);
364
+
365
+ };
366
+
367
+
368
+
369
+ const gapiRequest = (arg) =>
370
+
371
+ new Promise((resolve, reject) => {
372
+
373
+ gapi.client.request(arg).then(
374
+
375
+ (res) => {
376
+
377
+ resolve(res);
378
+
379
+ },
380
+
381
+ (err) => {
382
+
383
+ reject(err);
384
+
385
+ }
386
+
387
+ );
388
+
389
+ });
390
+
391
+
392
+
393
+ const onChange = (date, dateString) => {
394
+
395
+ const [startDate, endDate] = dateString;
396
+
397
+ setState({ ...state, startDate, endDate });
398
+
399
+ };
400
+
401
+
402
+
403
+ return (
404
+
405
+ <Spin spinning={settingLoading} tip="Loading...">
406
+
407
+ <div id="google-signin-button"></div>
408
+
409
+ {!signedIn ? (
410
+
411
+ <Button icon="google" onClick={onSignIn}>
412
+
413
+ SignIn
414
+
415
+ </Button>
416
+
417
+ ) : (
418
+
419
+ <Button icon="google" onClick={onSignOut}>
420
+
421
+ SignOut
422
+
423
+ </Button>
424
+
425
+ )}
426
+
427
+ <div className={"container"}>
428
+
429
+ <div className={"row"}>
430
+
431
+ <div className={analysisPeriod}>
432
+
433
+ <div>
434
+
435
+ <div className={analysisTitle}>
436
+
437
+ <Icon type="bar-chart" />
438
+
439
+ <span style={{ marginLeft: "2%" }}>アクセス解析</span>
440
+
441
+ </div>
442
+
443
+ <div className={analysisDate}>
444
+
445
+ <RangePicker
446
+
447
+ onChange={onChange}
448
+
449
+ defaultValue={[
450
+
451
+ moment("2019-10-31", dateFormat),
452
+
453
+ moment("2019-10-31", dateFormat),
454
+
455
+ ]}
456
+
457
+ />
458
+
459
+ </div>
460
+
461
+ </div>
462
+
463
+ </div>
464
+
465
+ <div className={access}>
466
+
467
+ <dl>
468
+
469
+ <dt>
470
+
471
+ <span>サイト訪問数</span>
472
+
473
+ </dt>
474
+
475
+ <dd id="users"></dd>
476
+
477
+ </dl>
478
+
479
+ </div>
480
+
481
+ <div className={access}>
482
+
483
+ <dl>
484
+
485
+ <dt>
486
+
487
+ <span>ページ閲覧数</span>
488
+
489
+ </dt>
490
+
491
+ <dd id="pageviews"></dd>
492
+
493
+ </dl>
494
+
495
+ </div>
496
+
497
+ </div>
498
+
499
+ </div>
500
+
501
+ </Spin>
502
+
503
+ );
504
+
505
+ };
506
+
507
+
508
+
509
+ export default Aaa;
510
+
511
+
512
+
17
513
  ```
18
514
 
19
- をIDベースで、
20
-
21
- returnして、
22
-
23
- ```react
24
-
25
- <div className={access}>
26
-
27
- <dl>
28
-
29
- <dt>
30
-
31
- <span>サイト訪問数</span>
32
-
33
- </dt>
34
-
35
- <dd id="users"></dd>
36
-
37
- </dl>
38
-
39
- </div>
40
-
41
- <div className={access}>
42
-
43
- <dl>
44
-
45
- <dt>
46
-
47
- <span>ページ閲覧数</span>
48
-
49
- </dt>
50
-
51
- <dd id="pageviews"></dd>
52
-
53
- </dl>
54
-
55
- </div>
56
-
57
- ```
58
-
59
- として表示しているのですが、
60
-
61
- これを仮想DOMで使いたい場合どういう実装をすればよろしいでしょうか?
62
-
63
- お手数ですが、よろしくお願いいたします、
64
-
65
-
66
-
67
-
68
-
69
- ### 発生している問題・エラーメッセージ
70
-
71
-
72
-
73
- ```
74
-
75
- エラーメッセージ
76
-
77
- ```
78
-
79
-
80
-
81
- ### 該当のソースコード
82
-
83
-
84
-
85
- ```react
86
-
87
- import React, { useState, useEffect } from "react";
88
-
89
- import { Spin, Button, Icon, DatePicker } from "antd";
90
-
91
- import moment from "moment";
92
-
93
-
94
-
95
- const { gapi } = window;
96
-
97
- const { RangePicker } = DatePicker;
98
-
99
- const dateFormat = "YYYY-MM-DD";
100
-
101
-
102
-
103
- const Aaa = () => {
104
-
105
- const [state, setState] = useState({
106
-
107
- viewId: "",
108
-
109
- clientId: "",
110
-
111
- startDate: "2222-22-01",
112
-
113
- endDate: "2222-22-01",
114
-
115
- users: "ga:users",
116
-
117
- pageviews: "ga:pageviews",
118
-
119
- });
120
-
121
- const [signedIn, setSignedIn] = useState(false);
122
-
123
-
124
-
125
- useEffect(() => {
126
-
127
- if (!settingData) return;
128
-
129
- const { gaConfig } = JSON.parse(settingData.setting.values);
130
-
131
- const { viewId, clientId } = JSON.parse(gaConfig);
132
-
133
- const f = async () => {
134
-
135
- setState({
136
-
137
- ...state,
138
-
139
- viewId,
140
-
141
- clientId,
142
-
143
- });
144
-
145
- };
146
-
147
- f();
148
-
149
- }, [settingData]);
150
-
151
-
152
-
153
- useEffect(() => {
154
-
155
- if (!state.clientId) return;
156
-
157
- const f = async () => {
158
-
159
- console.log(state.clientId);
160
-
161
- gapiLoad();
162
-
163
- };
164
-
165
- f();
166
-
167
- // eslint-disable-next-line react-hooks/exhaustive-deps
168
-
169
- }, [state.clientId]);
170
-
171
-
172
-
173
- // Query the API and print the results to the page.
174
-
175
- useEffect(() => {
176
-
177
- if (!signedIn) return;
178
-
179
- const f = async () => {
180
-
181
- await sendQuery();
182
-
183
- };
184
-
185
- f();
186
-
187
- // eslint-disable-next-line react-hooks/exhaustive-deps
188
-
189
- }, [signedIn, state.startDate, state.endDate]);
190
-
191
-
192
-
193
- if (settingError) {
194
-
195
- const error = settingError;
196
-
197
- return `Error! ${error}`;
198
-
199
- }
200
-
201
-
202
-
203
- const sendQuery = async () => {
204
-
205
- const arg = {
206
-
207
- path: "/v4/reports:batchGet",
208
-
209
- root: "https://analyticsreporting.googleapis.com/",
210
-
211
- method: "POST",
212
-
213
- body: {
214
-
215
- reportRequests: [
216
-
217
- {
218
-
219
- viewId: state.viewId,
220
-
221
- dateRanges: [
222
-
223
- {
224
-
225
- startDate: state.startDate,
226
-
227
- endDate: state.endDate,
228
-
229
- },
230
-
231
- ],
232
-
233
- metrics: [
234
-
235
- {
236
-
237
- expression: state.users,
238
-
239
- },
240
-
241
- {
242
-
243
- expression: state.pageviews,
244
-
245
- },
246
-
247
- ],
248
-
249
- },
250
-
251
- ],
252
-
253
- },
254
-
255
- };
256
-
257
-
258
-
259
- const res = await gapiRequest(arg).catch((err) => err);
260
-
261
- // invalid viewId
262
-
263
- if (res.result.error) {
264
-
265
- console.log(res.result.error);
266
-
267
- return;
268
-
269
- }
270
-
271
- // const formattedJson = JSON.stringify(res.result.reports[0], null, 2);
272
-
273
- const users = res.result.reports[0].data.totals[0].values[0];
274
-
275
- document.getElementById("users").textContent = users;
276
-
277
- const pageviews = res.result.reports[0].data.totals[0].values[1];
278
-
279
- document.getElementById("pageviews").textContent = pageviews;
280
-
281
- };
282
-
283
-
284
-
285
- const gapiLoad = async () => {
286
-
287
- gapi.load("auth2", async (signin2) => {
288
-
289
- gapi.signin2.render("google-signin-button", {
290
-
291
- onsuccess: (res) => {
292
-
293
- console.log(res);
294
-
295
- setSignedIn(true);
296
-
297
- },
298
-
299
- onfailure: (res) => {
300
-
301
- console.log(res.error);
302
-
303
- // setSignedIn(true)
304
-
305
- },
306
-
307
- });
308
-
309
- const res = await gapi.auth2
310
-
311
- .init({
312
-
313
- client_id: state.clientId,
314
-
315
- scope: "https://www.googleapis.com/auth/analytics.readonly",
316
-
317
- })
318
-
319
- .then((res) => res)
320
-
321
- .catch((err) => err);
322
-
323
- if (res.error) {
324
-
325
- console.log(res.error);
326
-
327
- return;
328
-
329
- }
330
-
331
- });
332
-
333
- };
334
-
335
-
336
-
337
- const onSignIn = async () => {
338
-
339
- const auth = gapi.auth2.getAuthInstance();
340
-
341
- const res = await auth.signIn().catch((err) => err);
342
-
343
- if (res.error) {
344
-
345
- console.log(res.error);
346
-
347
- return;
348
-
349
- }
350
-
351
- setSignedIn(true);
352
-
353
- };
354
-
355
-
356
-
357
- const onSignOut = () => {
358
-
359
- const auth = gapi.auth2.getAuthInstance();
360
-
361
- auth.signOut();
362
-
363
- setSignedIn(false);
364
-
365
- };
366
-
367
-
368
-
369
- const gapiRequest = (arg) =>
370
-
371
- new Promise((resolve, reject) => {
372
-
373
- gapi.client.request(arg).then(
374
-
375
- (res) => {
376
-
377
- resolve(res);
378
-
379
- },
380
-
381
- (err) => {
382
-
383
- reject(err);
384
-
385
- }
386
-
387
- );
388
-
389
- });
390
-
391
-
392
-
393
- const onChange = (date, dateString) => {
394
-
395
- const [startDate, endDate] = dateString;
396
-
397
- setState({ ...state, startDate, endDate });
398
-
399
- };
400
-
401
-
402
-
403
- return (
404
-
405
- <Spin spinning={settingLoading} tip="Loading...">
406
-
407
- <div id="google-signin-button"></div>
408
-
409
- {!signedIn ? (
410
-
411
- <Button icon="google" onClick={onSignIn}>
412
-
413
- SignIn
414
-
415
- </Button>
416
-
417
- ) : (
418
-
419
- <Button icon="google" onClick={onSignOut}>
420
-
421
- SignOut
422
-
423
- </Button>
424
-
425
- )}
426
-
427
- <div className={"container"}>
428
-
429
- <div className={"row"}>
430
-
431
- <div className={analysisPeriod}>
432
-
433
- <div>
434
-
435
- <div className={analysisTitle}>
436
-
437
- <Icon type="bar-chart" />
438
-
439
- <span style={{ marginLeft: "2%" }}>アクセス解析</span>
440
-
441
- </div>
442
-
443
- <div className={analysisDate}>
444
-
445
- <RangePicker
446
-
447
- onChange={onChange}
448
-
449
- defaultValue={[
450
-
451
- moment("2019-10-31", dateFormat),
452
-
453
- moment("2019-10-31", dateFormat),
454
-
455
- ]}
456
-
457
- />
458
-
459
- </div>
460
-
461
- </div>
462
-
463
- </div>
464
-
465
- <div className={access}>
466
-
467
- <dl>
468
-
469
- <dt>
470
-
471
- <span>サイト訪問数</span>
472
-
473
- </dt>
474
-
475
- <dd id="users"></dd>
476
-
477
- </dl>
478
-
479
- </div>
480
-
481
- <div className={access}>
482
-
483
- <dl>
484
-
485
- <dt>
486
-
487
- <span>ページ閲覧数</span>
488
-
489
- </dt>
490
-
491
- <dd id="pageviews"></dd>
492
-
493
- </dl>
494
-
495
- </div>
496
-
497
- </div>
498
-
499
- </div>
500
-
501
- </Spin>
502
-
503
- );
504
-
505
- };
506
-
507
-
508
-
509
- export default Aaa;
510
-
511
-
512
-
513
- ```
514
-
515
515
 
516
516
 
517
517
  ### 試したこと

1

タイトル変更

2020/05/19 07:34

投稿

kaoru_tujimiya
kaoru_tujimiya

スコア36

test CHANGED
@@ -1 +1 @@
1
- 普通のDOMから仮想DOMに変更する方法 React
1
+ 【React】普通のDOMから仮想DOMに変更する方法
test CHANGED
File without changes