teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

文法の修正

2020/12/31 12:08

投稿

ryo-0213
ryo-0213

スコア2

title CHANGED
@@ -1,1 +1,1 @@
1
- jsonファイルのuserName表示させたい
1
+ useStateを使ってjsonファイルの中身出力したい
body CHANGED
File without changes

1

質問内容の変更

2020/12/31 12:08

投稿

ryo-0213
ryo-0213

スコア2

title CHANGED
File without changes
body CHANGED
@@ -1,84 +1,37 @@
1
- ### jsonファイルのuserName表示させたい
1
+ ### useStateを使ってjsonファイルの中身出力したい
2
2
 
3
+ ### 発生している問題
3
4
 
4
- ### 発生している問題・エラーメッセージ
5
- ```
6
- TypeError: Cannot read property 'toLowerCase' of null
7
- ```
8
- 原因だと思われるコード
9
- 色々と書き直しましたがうまく動きませんでした。
10
- ```
11
- setOptions(
12
- Object.keys(userprofiles).map(
5
+ userprofilesには入っているのですが、optionsが空になっています
13
- (key) => userprofiles[key]
6
+ ![イメージ説明](e958a65b0c360e17b13234a78cf1eb89.png)
14
- ) as UserProfile[]
15
- );
16
- ```
17
-
18
7
  ### 該当のソースコード
19
8
 
20
9
  ```
21
10
  interface UserProfile {
22
11
  userName: string;
12
+ }
23
13
 
24
14
  export default function Asynchronous() {
25
15
  const [options, setOptions] = useState<UserProfile[]>([]);
26
-
16
+
27
17
  useEffect(() => {
28
18
  let active = true;
29
19
 
30
- if (!loading) {
31
- return undefined;
32
- }
33
-
34
20
  (async () => {
35
21
  const response = await fetch("http://localhost:8080/api/v1/userprofile");
36
- await sleep(1e3);
37
22
  const userprofiles = await response.json();
38
- // keys(o: object): string[];
23
+ console.log(userprofiles);
39
24
  if (active) {
40
25
  setOptions(
41
26
  Object.keys(userprofiles).map(
42
- (key) => userprofiles[key]
27
+ (key) => userprofiles[key] ) as UserProfile[]
43
- ) as UserProfile[]
44
28
  );
45
29
  }
46
30
  })();
47
- return () => {
48
- active = false;
49
- };
50
- }, [loading]);
51
31
 
52
- return (
53
- <Autocomplete
54
- id="demo"
55
- getOptionSelected={(option, value) => option.userName === value.userName}
56
- getOptionLabel={(option) => option.userName}
57
- options={options}
32
+ console.log(options);
58
- loading={loading}
59
- renderInput={(params) => (
60
- <TextField
61
- {...params}
62
- label="検索"
63
- variant="outlined"
64
- InputProps={{
65
- ...params.InputProps,
66
- endAdornment: (
67
- <React.Fragment>
68
- {loading ? (
69
- <CircularProgress color="inherit" size={20} />
70
- ) : null}
71
- {params.InputProps.endAdornment}
72
- </React.Fragment>
73
- ),
74
- }}
75
- />
76
- )}
77
- />
78
- );
79
- }
80
-
81
33
  ```
34
+ optionsで出力しようとしているのですがうまく行きません。
82
35
  ### 補足情報(FW/ツールのバージョンなど)
83
36
  "typescript": "^4.1.3",
84
37
  "react": "^17.0.1",