I will have product filter on ecommerce page. I'm not sure how can I pass dynamic number of parameters in my query? Because sometimes will query use 1 parameter sometimes 6 and more.
Example
const fetchProducts = async (size, color, weight, deep,) => {
const parsed = await ky(
`http://localhost:3000/api/products/get?size=${size}&color=${color}`
).json();
return parsed;
};
const { data, status } = useQuery([size, color,], () => fetchPosts(size, color));
Sometimes will be 10 different parameters from the product filter, sometimes just 1...
How can I handle this dynamically? I will need then put the filter on prisma backend.
You can use qs package to handle all params.
Example:
const fetchProducts = (params)=>{
const query = qs.stringify(params);
const baseUrl = "http://localhost:3000/api/products/get?";
const parsed = await ky(baseUrl + query);
....
}
const params = {color:"red", size:"big"}
const query = qs.stringify(params);
//output: "color=red&size=big"