【发布时间】:2021-08-02 10:22:36
【问题描述】:
我无法在同一个 .js 文件中的功能组件中传递道具。我之前尝试过发布此问题,但没有任何帮助。
我想在我的第一个函数 GetMemID 中从 GET 请求中获取 member_id,并使用它在我的第二个函数 Transactions 中设置我的 useState 的 member_id。
我知道我的 GET 请求正在运行,因为我能够在检查我的代码后看到数据。
到目前为止,使用我的代码,当我在 Transactions 中显示 member_id 时,我得到了“[object Object]”。
这是我目前的代码:
import React, { useEffect, useState } from 'react';
import { Redirect } from 'react-router-dom';
import moment from 'moment';
import HomeNavBar from '../components/HomeNavBar.js';
var currentDate = moment().format("MM/DD/YYYY HH:mm:ss");
function GetMemID() {
const [details, setDetails] = useState([]);
const [error, setError] = useState(null);
const options = {
method: 'GET',
headers: {
'Content-type': 'application/json; charset=UTF-8',
'Accept': 'application/json',
'Authorization': `JWT ${localStorage.getItem('token')}`
}
};
useEffect(() => {
fetch("http://########/api/members/get/", options)
.then(response => {
if (response.status !== 200) {
console.log(response.status);
setError(response);
}
response.json().then(data => {
setDetails(data);
});
});
}, []);
return (
<div className="App">
{details.map(item => (
<Transaction member_id={item.member_id} />
))}
</div>
);
}
function Transaction({member_id}) {
//props.state
const [error, setError] = useState(null);
const [trans, setTrans] = useState({member_id:member_id, category:'', description:'', amount:0});
const [details, setDetails] = useState({id:0, mmeber_id:0, group:"", username:""});
console.log("member_id: " + member_id);
//GET rerquest to get transaction memberID
const options = {
method: 'GET',
headers: {
'Content-type': 'application/json; charset=UTF-8',
'Accept': 'application/json',
'Authorization': `JWT ${localStorage.getItem('token')}`
},
};
useEffect(() => {
fetch("http://########/api/members/get/", options)
.then(response => {
if (response.status !== 200) {
console.log(response.status);
setError(response);
}
response.json().then(data => {
setDetails(data);
});
});
}, []);
//POST request to API for transaction
const optionPOST = {
method: 'POST',
headers: {
'Content-type': 'application/json; charset=UTF-8',
'Accept': 'application/json',
'Authorization': `JWT ${localStorage.getItem('token')}`
},
body:JSON.stringify(trans)
}
const createTransaction = e => {
e.preventDefault();
fetch("http://########/api/transactions/post/", optionPOST)
.then((response) => console.log('reponse: ' + response.json()))
.then((message) => console.log('message: ' + message))
}
if (error) {
return (<Redirect to="/Signin" />);
} else{
return (
<div className="wrapper">
<HomeNavBar />
<form>
<div className="form-horizantal">
<fieldset>
<div className="form-group row">
<label className="col-md-12"><p>{currentDate}</p></label>
</div>
<div className="form-group row">
<label className="col-md-12">
<p>Member ID</p>
<input type="text" name="member_id" defaultValue={trans.member_id} readOnly/>
</label>
</div>
<div className="form-group row">
<label className="col-md-12">
Select Category
</label>
</div>
<div className="form-group row">
<label className="col-md-12">
<select value={trans.category} onChange={e => setTrans({ ...trans, category: e.target.value })} >
<option value=""></option>
<option value="billsutilities">Bills/Utilities</option>
<option value="groceries">Groceries</option>
<option value="health">Health</option>
<option value="transportation">Transportation</option>
<option value="ehoppingentertainment">Shopping/Entertainment</option>
<option value="mics.">Mics.</option>
</select>
</label>
</div>
<div className="form-group row">
<label className="col-md-12">
<p>Description</p>
<input type="text" name="description" value={trans.description} onChange={e => setTrans({ ...trans, description: e.target.value })} />
</label>
</div>
<div className="form-group row">
<label className="col-md-12">
<p>Amount</p>
<input type="text" name="amount" value={trans.amount} onChange={e => setTrans({ ...trans, amount: e.target.value })} />
</label>
</div>
</fieldset>
<button type="submit" onClick={createTransaction}>Submit</button>
</div>
</form>
</div>
);
}
}
export default Transaction;
【问题讨论】:
-
API 中的数据结构如何?
-
在
<Transaction member_id={item.member_id} />之前记录item的值 -
console.log("member_id: " + member_id);function Transaction({member_id}) {下面的第四行在控制台中显示什么? -
@kayuapi_my,它显示“member_id: undefined”
-
@WebbH,格式为 JSON。
标签: javascript reactjs components undefined react-functional-component