質問編集履歴

1

コードを記載しました。

2022/06/10 03:20

投稿

shunta80
shunta80

スコア96

test CHANGED
File without changes
test CHANGED
@@ -8,12 +8,147 @@
8
8
  この2つの使い分けやできればこっちを使ったほうがいいなど
9
9
  大きな違いはあるのでしょうか。
10
10
 
11
+ ### **その状況が発生しているコード**
12
+
13
+ **カスタムフック**
14
+ ```
15
+ import { useEffect, useMemo, useState } from "react";
16
+ import { useNavigate } from 'react-router';
17
+ import { useCallback } from 'react';
18
+ import axios from 'axios';
19
+
20
+ const useProvider =(props) => {
21
+
22
+ const[data,setdata]=useState([])
23
+
24
+ useEffect(()=>{
25
+ axios.get('http://webservice.recruit.co.jp/hotpepper/large_area/v1/?format=json&key=***')
26
+
27
+ .then(function(nn){
28
+ setdata(nn.data.results.large_area)
29
+ })
30
+ .catch(error => console.log(error))
31
+
32
+ },[])
33
+
34
+ const history=useNavigate()
35
+
36
+ const branch1=useCallback((t)=>{
37
+ switch (t.target.value) {
38
+ case 10:
39
+ history('/North')
40
+ break;
41
+ case 20:
42
+ history('/East')
43
+ break;
44
+ case 30:
45
+ history('/West')
46
+ break;
47
+
48
+ default:
49
+ return;
50
+
51
+ }
52
+ },[])
53
+
54
+ const rock= {branch1,data}
55
+
56
+ return rock
57
+ };
58
+
59
+ export default useProvider;
60
+ ```
61
+
62
+ **useContext**
63
+ ```
64
+ import { createContext, useEffect, useMemo, useState } from "react";
65
+ import { useNavigate } from 'react-router';
66
+ import { useCallback } from 'react';
67
+ import axios from 'axios';
68
+
69
+ export const Context = createContext();
70
+
71
+ const Provider =(props) => {
72
+
73
+ const { children } = props;
74
+ const[data2,setdata2]=useState([])
75
+
76
+ useEffect(()=>{
77
+ axios.get('http://webservice.recruit.co.jp/hotpepper/large_area/v1/?format=json&key=***')
78
+
79
+ .then(function(nn){
80
+ setdata2(nn.data.results.large_area)
81
+ })
82
+ .catch(error => console.log(error))
83
+
84
+ },[])
85
+
86
+ const history=useNavigate()
87
+
88
+ const branch2=useCallback((t)=>{
89
+
90
+ switch (t.target.value) {
91
+ case 10:
92
+ history('/North')
93
+ break;
94
+ case 20:
95
+ history('/East')
96
+ break;
97
+ case 30:
98
+ history('/West')
99
+ break;
100
+
101
+ default:
102
+ return;
103
+
104
+ }
105
+ },[])
106
+
107
+
108
+ return (
109
+ <Context.Provider value={{branch2,data2}}>
110
+ {children}
111
+ </Context.Provider>
112
+ )
113
+ };
114
+
115
+ export default Provider;
116
+ ```
117
+
118
+ **関数を使うファイル**
119
+ ```
120
+ import { memo } from "react";
121
+ import Menu from '@mui/material/Menu';
122
+ import MenuItem from '@mui/material/MenuItem';
123
+ import style from '../style.module.css'
124
+
125
+ import { Context } from "./globalstate";
126
+ import { useContext } from "react";
127
+
128
+ import useProvider from "./customfook";
129
+
130
+ const Preselect=memo((props)=>{
131
+
132
+ // useContextから呼び出し
133
+ const {branch2,data2}=useContext(Context)
134
+
135
+ // カスタムフックから呼び出し
136
+ const rock=useProvider()
137
+
138
+ return(
139
+ <>
140
+ <button onClick={rock.branch1} value={10}>ボタン1</button>
141
+ <button onClick={branch2} value={20}>ボタン2</button>
142
+ <button onClick={branch2} value={30}>ボタン3</button>
143
+ </>
144
+ )
145
+ })
146
+
147
+ export default Preselect;
148
+ ```
11
149
 
12
150
 
13
151
 
14
152
 
15
153
 
16
154
 
17
-
18
-
19
-