TypeScript
TL;DR
Developers are dealing with an infinite loop issue in a React component where the useEffect hook is triggering a state change. The problem arises due to the dependency array in the useEffect hook. To solve this, developers should remove 'owner' from the dependency array.TypeScript
const Thread = ({ thread, setthraeds, onuserdata }) => {
const [loading, setloading] = useState(true);
const [owner, setowner] = useState(null);
const [threadinstances, setthreadinstace] = useState(thread);
const handledelete = async (e) => {
e.preventDefault();
await database.deleteDocument(VITE_DBID, VITE_COLLECTIONID, thread.$id);
setthraeds(prevstate => prevstate.filter(item => item.$id !== thread.$id));
};
let createdate = thread.$createdAt;
useEffect(() => {
const fetchUserData = async () => {
const payload = { 'owner_id': thread.owner_id };
try {
const response = await functions.createExecution('65e6b4bd0baa96e174fc', JSON.stringify(payload));
const profile = await database.getDocument(VITE_DBID, VITE_COLLECTIONID_USERPROF, thread.owner_id);
const userdata = JSON.parse(response.responseBody);
userdata.Profile_pic = profile.Profile_pic;
setowner(userdata);
onuserdata(userdata);
setloading(false);
} catch (error) {
console.log(error);
}
};
fetchUserData();
}, [owner, functions, database, VITE_DBID, VITE_COLLECTIONID_USERPROF, onuserdata]);
const toggleLike = async () => {
const user_who_like = thread.user_who_like;
const currentuserid = thread.owner_id;
if (user_who_like.includes(currentuserid)) {
const index = user_who_like.indexOf(currentuserid);
user_who_like.splice(index, 1);
} else {
user_who_like.push(currentuserid);
}
const payload = {
'user_who_like': user_who_like,
'likes': user_who_like.length
};
const response = await database.updateDocument(VITE_DBID, VITE_COLLECTIONID, thread.$id, payload);
setthreadinstace(response);
};
if (loading) return null;
return (
export default Thread;
Recommended threads
- custom domain on cloudflair was verified...
I've added the credentials as instructed by the appwrite documentation, with cname set to dns only without proxy, and it successfully validated my domain, and w...
- Unable to connect the git repo with appw...
I am trying to connect my existing repo with the appwrite functions. I have 2 functions in the same repo, and i was able to execute those through cli and its vi...
- Substring lookup for array elements
Hello, Is substring look up possible for array of strings? ```js Query.contains('product_names', [chair]), ``` this only works if I have the exact item name. `...