英文:
Check if a user is already following, then change button text to unfollow
问题
我已经成功让用户在React中关注和取消关注彼此,现在我面临的唯一障碍是相应地更改按钮。这是我的API的基本结构:
我有一个"followers"字段,其中包含一个关注我的用户数组,如何检查已登录用户是否在该数组中,然后更改他们的按钮为"取消关注",否则按钮将显示"关注"。
我从本地存储的JWT中获取已登录用户的user_id:
const token = localStorage.getItem('authTokens');
const decoded = jwtDecode(token);
const user_id = decoded.user_id;
这些是按钮:
<div onClick={follow} className="follow-btn color2-bg">
关注 <i className="fas fa-user-plus" />
</div>
<div onClick={unfollow} className="follow-btn color2-bg bg-danger">
取消关注 <i className="fas fa-user-minus text-white" />
</div>
这是我的useState的外观:
const [profile, setProfile] = useState({
user: '',
full_name: '',
...
followers: '',
});
因此,如果我想要获取正在查看其个人资料的用户的full_name,我可以使用 profile.full_name
。
我将提供更多需要的信息或细节。
编辑:这是如何将值添加到本地存储的方式:
// 这将用户的关注者保存到本地存储
const response = await api.get(baseUrl + '/service-provider/' + profile_ID + '/')
localStorage.setItem("followers", JSON.stringify(response.data.followers))
如何在页面刷新后保留文本 (关注或取消关注)?
英文:
I have finally made it so that users can follow and unfollow each other in react, now the only obstacle I am left with is changing the button accordingly. This is the basic structure of my API:
{
"id":4,
"user": {
"id": 4,
"username": "sammy"
},
"full_name": "Sam Iyke",
"bio": "Software Enginner",
"image": "http://127.0.0.1:8000/media/user_images/ins-gallery_5.jpg",
"followers": [
{
"id": 1,
"username": "destiny",
"email": "desphixs@gmail.com"
},
{
"id": 5,
"username": "jenny",
"email": "jenny@gmail.com"
}
],
"date": "2023-02-16T00:41:46.147589Z"
}
I have a followers field there, that have an array of users that are following me, how do i check if the logged in user is in the array, then change thier button to Unfollow, else the button would be Follow.
I am getting the logged in user_id from localstorage jwt
const token = localStorage.getItem('authTokens');
const decoded = jwtDecode(token);
const user_id = decoded.user_id;
These are the button
<div onClick={follow} className="follow-btn color2-bg">
Follow <i className="fas fa-user-plus" />
</div>
<div onClick={unfollow} className="follow-btn color2-bg bg-danger">
Unfollow <i className="fas fa-user-minus text-white" />
</div>
This is how my useState looks like
const [ profile, setProfile ] = useState({
user: '',
full_name:"",
...
followers:"",
});
So if I want to get the full_name of the user that we are viewing their profile, I do profile.full_name
I would provide any more information or detail needed.
Edit
This is how i add the values to the localstorage
// this saves the user followers to the localstorage
const response = await api.get(baseUrl + '/service-provider/' + profile_ID + '/')
localStorage.setItem("followers", JSON.stringify(response.data.followers))
function followToggle (){
if(isFollowing){
//remove follower
const followers = JSON.parse(localStorage.getItem("followers"))
let remaining = followers.filter(user=>user.id!==user_id);
localStorage.setItem("remaining" ,JSON.stringify(remaining))
setIsFollowing(false)
}
else{
//add follower
const followers = JSON.parse(localStorage.getItem("followers"))
followers.push(user_id);
localStorage.setItem("followers" ,JSON.stringify(followers))
setIsFollowing(true)
}
}
How to make the Text (Follow or Unfollow) retained after page refreshes?
答案1
得分: 1
首先,你可以创建另一个 state
变量来知道用户是否正在关注他或不关注他,或者可以手动执行以下步骤:
let userIndex = profile.followers.findIndex(i => i.id === currentUserId);
const [isFollowing, setIsFollowing] = useState(userIndex > -1);
然后在你的代码中,根据 isFollowing
的值显示不同的按钮:
{!isFollowing ? <div onClick={followToggle} className="follow-btn color2-bg">
关注 <i className="fas fa-user-plus" />
</div> : <div onClick={followToggle} className="follow-btn color2-bg bg-danger">
取消关注 <i className="fas fa-user-minus text-white" />
</div>}
最后,你的 followToggle
函数可以如下所示:
function followToggle() {
if (isFollowing) {
// 取消关注
let followers = // 从本地存储获取关注者数组
let remaining = followers.filter(user => user.id !== currentUserId);
// 将 remaining 保存到本地存储中
} else {
// 关注
let followers = // 从本地存储获取关注者数组
followers.push(currentUser);
// 将 followers 保存到本地存储中
}
}
请注意,上述代码中的注释部分需要根据你的具体应用程序逻辑来填充,以确保它们与你的应用程序的数据和本地存储操作一致。
英文:
first of all you can create another state
to know the user is following him or not, or can do it manually
let userIndex= profile.followers.findIndex(i=>i.id===currentUserId)
const [isFollowing,setIsFollowing]=useState(userIndex > -1);
then in your code
{!isFollowing ? <div onClick={followToggle} className="follow-btn color2-bg">
Follow <i className="fas fa-user-plus" />
</div>
:<div onClick={followToggle} className="follow-btn color2-bg bg-danger">
Unfollow <i className="fas fa-user-minus text-white" />
</div>
}
then your function wil be like this
function followToggle (){
if(isFollowing){
//remove follower
let followers = //from local storage it should be an array
let remaining = followers.filter(user=>user.id!==currentUserId);
//set remaining to local storage
}
else{
//add follower
let followers = //from local storage it should be an array
followers.push(currentUser);
//set followers to local storage
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论