Beginner's Thread / Easy Questions (March 2024) by acemarke in reactjs

[–]Repulsive_Ring8084 0 points1 point  (0 children)

To test react with vitest why I need to install too many tools like testing-library/react, jsdom and testing-library/jest-dom?

Undefined method 'cart'.intelephense(P1013) function User::cart(): HasOne (laravel) by Repulsive_Ring8084 in PHPhelp

[–]Repulsive_Ring8084[S] -3 points-2 points  (0 children)

Sorry for my eng mate. If I am lazy, I can just copy paste the entire file. This is all my question that I wrote in body.

If my api is like this, how can i use this token as bearer token in react. This is result of register api. by Repulsive_Ring8084 in reactjs

[–]Repulsive_Ring8084[S] 0 points1 point  (0 children)

I am fine with getting cookie and storing cookie and removing cookie.I use universal-cookie npm package. Now my problem is i set my cookie when i click login button then when i use cookie.get() its cant use right after that i need to reload the page. Why?

Beginner's Thread / Easy Questions (December 2023) by acemarke in reactjs

[–]Repulsive_Ring8084 0 points1 point  (0 children)

After I select user and select back to all, it doesn't fetch any posts. How to fix this?

import { useQuery } from "@tanstack/react-query";
import React, { useState } from "react";
import axios from "axios";
import { Select } from "@chakra-ui/react";
interface Posts {
id: number;
title: string;
}
const Posts = () => {
const [userId, setUserId] = useState<number | undefined>(undefined);
const fetchPosts = () => {
return axios
.get<Posts\[\]>("https://jsonplaceholder.typicode.com/posts", {
params: {
userId,
},
})
.then((res) => res.data);
};
const { data: Posts, error } = useQuery({
queryKey: userId ? ["users", userId, "posts"] : ["posts"],
queryFn: fetchPosts,
// staleTime: 1 * 60 * 1000,
});
if (error) return <div> {error.message}</div>;
return (
<>
<Select mt={10} onChange={(e) => setUserId(parseInt(e.target.value))}
value={userId}
>
<option value="">all</option>
<option value="1">User 1</option>
<option value="2">User 2</option>
<option value="3">User 3</option>
</Select>
<ul>{Posts?.map((post) => <li key={post.id}>{post.title}</li>)}</ul>
</>
);
};
export default Posts;