Javascript 选择后反应<;李>;项目';身份证
我想点击Javascript 选择后反应<;李>;项目';身份证,javascript,reactjs,react-hooks,Javascript,Reactjs,React Hooks,我想点击项目的ID,并用Fetch发布该值。我不知道如何处理它。我在等你的帮助 我在这张照片上解释了我的代码。 基本上,我可以在项中列出所有用户列表。它起作用了!但我需要点击项目的“user.id”并保存到“setClickedUser”这样的状态。然后用Fetch发布这个ID const[username,setUsername]=useState([]); const[conversatid,setConversatId]=useState([]); useffect(()=>{ 取('h
项目的ID,并用Fetch发布该值。我不知道如何处理它。我在等你的帮助
我在这张照片上解释了我的代码。
基本上,我可以在
项中列出所有用户列表。它起作用了!但我需要点击项目的“user.id”并保存到“setClickedUser”这样的状态。然后用Fetch发布这个ID
const[username,setUsername]=useState([]);
const[conversatid,setConversatId]=useState([]);
useffect(()=>{
取('http://localhost:8000/api/current_user/', {
标题:{
授权:`JWT${localStorage.getItem('token')}`
}
})
.then(res=>res.json())
。然后(json=>{
setUsername(json.id);
});
},[])
const convcreate=(e,data,kullanici)=>{
取('http://localhost:8000/api/conversat/', {
方法:“POST”,
标题:{
授权:`JWT${localStorage.getItem('token')}`,
“内容类型”:“应用程序/json”,
},
正文:JSON.stringify({User1i:username,User2ii:clickeduser,})
})
.then(res=>res.json())
。然后(json=>{
setConversatId(json.id);
}); };
const=user=>evt=>{
///获取单击的用户ID并保存到“clickeduser”
}
返回(
{props.allusers2&&props.allusers22.map(用户=>{
返回(
{user.username}
);}}
您可以执行以下操作:
const alluClicked = userId => {
// SAVE userId where you need
}
return (
<div className="kullist">
{props.allusers2 && props.allusers22.map(user => {
return (
<li key={user.id} onClick={() => alluClicked(user.id)} className="tumtekkul">
const=userId=>{
//在需要的地方保存userId
}
返回(
{props.allusers2&&props.allusers22.map(用户=>{
返回(
allucked(user.id)}className=“tumtekkul”>
您可以执行以下操作:
const alluClicked = userId => {
// SAVE userId where you need
}
return (
<div className="kullist">
{props.allusers2 && props.allusers22.map(user => {
return (
<li key={user.id} onClick={() => alluClicked(user.id)} className="tumtekkul">
const=userId=>{
//在需要的地方保存userId
}
返回(
{props.allusers2&&props.allusers22.map(用户=>{
返回(
allucked(user.id)}className=“tumtekkul”>
@strd4605@5458200
我试过你的密码
const alluClicked = userId => {
setUserRec(userId);
convcreate(userRec); */// Fetch Post function, you can see this above*
}
但是当我尝试发送这个用户ID“body:JSON.stringify({User1i:username,User2ii:userRec,})”时,
“我得到的是User2ii:[“这个字段是必需的。”]
这个响应
这太奇怪了,因为我可以用这个{userRec}
@strdr4605@5458200来显示点击的用户ID
我试过你的密码
const alluClicked = userId => {
setUserRec(userId);
convcreate(userRec); */// Fetch Post function, you can see this above*
}
但是当我尝试发送这个用户ID“body:JSON.stringify({User1i:username,User2ii:userRec,})”时,
“我得到的是User2ii:[“这个字段是必需的。”]
这个响应
这太奇怪了,因为我可以用这个
{userRec}
来显示点击过的用户ID。我已经解决了这个问题。我希望它能帮助另一个用户
const alluClicked = userId => {
fetch('http://localhost:8000/api/conversat/', {
method: 'POST',
headers: {
Authorization: `JWT ${localStorage.getItem('token')}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({User1i: username, User2ii: userId,})
})
.then(res => res.json())
.then(json => {
setConversatId(json.id);
});
}
我已经解决了那个问题。我希望它能帮助另一个
const alluClicked = userId => {
fetch('http://localhost:8000/api/conversat/', {
method: 'POST',
headers: {
Authorization: `JWT ${localStorage.getItem('token')}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({User1i: username, User2ii: userId,})
})
.then(res => res.json())
.then(json => {
setConversatId(json.id);
});
}
setUserRec(userId);
是异步的,您不能访问下一行的新状态!!!您可以使用useffect
在userRec
更改时进行提取调用。setUserRec(userId);
是异步的,您无法访问下一行的新状态!!!您可以使用useffect
在userRec
更改时进行提取调用。