質問編集履歴

2

修正

2022/11/27 23:57

投稿

caren
caren

スコア25

test CHANGED
File without changes
test CHANGED
@@ -35,7 +35,7 @@
35
35
 
36
36
  return(
37
37
  <TextField variant="standard" label="自社担当者" sx={{ m: 1 }}
38
- value={nohinHeader.representative} />
38
+ defaultValue={nohinHeader.representative} />
39
39
 
40
40
  );
41
41
  }
@@ -105,8 +105,7 @@
105
105
 
106
106
 
107
107
  ### 試したこと
108
-
109
- label="担当者"方のTextFieldに対してdefaultValueではなくvalue={test}とするとカスタムフックから取得した値はきちんと取れてきている。
108
+ のTextFieldに対してdefaultValueではなくvalue={nohinHeader.representative}とするとカスタムフックから取得した値はきちんと取れてきている。
110
109
  ### 補足情報(FW/ツールのバージョンなど)
111
110
 
112
111
  "@mui/material": "^5.10.8",

1

ソースコードの追加

2022/11/27 23:45

投稿

caren
caren

スコア25

test CHANGED
File without changes
test CHANGED
@@ -23,20 +23,86 @@
23
23
 
24
24
  ```test.jsx
25
25
 
26
+ import { NohinHeaderContext } from '../../providers/setNohinHeaderProvider';
27
+ import { useGetNohinHeaderInfo } from '../../../hooks/useGetNohinheaderInfo';
28
+ import {TextField} from '@mui/material';
29
+
30
+ const { nohinHeader, setNohinHeader } = useContext(NohinHeaderContext);
31
+
32
+ useGetNohinHeaderInfo();
33
+
26
- export const Test =() =>{
34
+ export const test =() =>{
27
- const { dialogStatus, setDialogStatus } = useContext(CustomerDialogContext);
28
- const test = nohinHeader["representative"]; //自作のカスタムフックから値を取得
29
35
 
30
36
  return(
31
- <TextField id="customerid" variant="standard" label="お客様コード" defaultValue={dialogStatus.mode == "edit" ? dialogStatus.rowData.customerid : ""}
32
- InputProps={{ disabled: dialogStatus.mode == "edit" ? true : false }} />
37
+ <TextField variant="standard" label="自社担当者" sx={{ m: 1 }}
38
+ value={nohinHeader.representative} />
39
+
40
+ );
41
+ }
33
42
 
43
+ ```
44
+
45
+ ```useGetNohinheaderInfo.js
34
- <TextField variant="standard" label="担当者" sx={{ m: 1 }}
46
+ import { useContext, useEffect } from "react";
47
+ import {NohinHeaderContext} from '../components/providers/setNohinHeaderProvider';
48
+
49
+ export const useGetNohinHeaderInfo =() =>{
50
+
51
+ const {nohinHeader, setNohinHeader}=useContext(NohinHeaderContext);
52
+ let newData = {};
35
- defaultValue={test}
53
+ useEffect(() => {
54
+ fetch('getnohinheaderinfo.php', { method: 'POST', mode: 'cors', credentials: 'include' }
55
+ )
56
+ .then(response => {
57
+ if (!response.ok) {
58
+ throw new Error(`${response.status} ${response.statusText}`);
59
+ }
60
+ return response.json();
61
+ })
36
- />
62
+ .then(data => {
63
+ console.log('** fetch: then(data)');
64
+ console.log(data);
65
+ newData={indexno:data.index_no,voucherno:data.voucher_no,customerid:data.customer_id,issueddate:data.issued_date,
66
+ representative:data.representative,taxr:data.tax_r,taxs:data.tax_s,subtotals:data.subtotal_s,subtotalr:data.subtotal_r,
67
+ taxamounts:data.tax_amount_s,taxamountr:data.tax_amount_r,totalnotax:data.total_notax,taxamount:data.tax_amount,totalamount:data.total_amount}
68
+ setNohinHeader(newData);
69
+ })
70
+ .catch((reason) => {
71
+ console.log('** fetch: catch');
72
+ console.log(reason);
37
- );
73
+ });
74
+ }, []);
75
+
76
+ return true;
38
77
  }
39
78
  ```
79
+
80
+ ```setNohinHeaderProvider.jsx
81
+
82
+ import { createContext, useState } from "react";
83
+
84
+ export const NohinHeaderContext = createContext({});
85
+
86
+ export const SetNohinHeaderProvider = props => {
87
+ const { children } = props;
88
+
89
+ const [nohinHeader, setNohinHeader] = useState({
90
+ indexno:"",voucherno: "", customerid: "", issueddate: "", representative: "",
91
+ taxr: "",taxs:"",subtotals:"",subtotalr:"",taxamounts:"",taxamountr:"",
92
+ totalnotax:"",taxamount:"",totalamount:"",
93
+ });
94
+
95
+ return (
96
+ <NohinHeaderContext.Provider value={{ nohinHeader, setNohinHeader}}>
97
+ {children}
98
+ </NohinHeaderContext.Provider>
99
+ );
100
+
101
+ };
102
+
103
+ ```
104
+
105
+
40
106
 
41
107
  ### 試したこと
42
108