React useEffect problem

~ useEffect(() => { const fetchPins = async () => { setLoading(true); setError(null); try { let query; if (text === "created") { console.log("Running userCreatedPinsQuery"); query = userCreatedPinsQuery(userId); } else { console.log("Running userSavedPinsQuery"); query = userSavedPinsQuery(userId); } console.log("Generated query:", query); const data = await client.fetch(query); console.log("Fetched data:", data); setPins(data); } catch (err) { console.error("Error fetching pins:", err); setError(err); } finally { setLoading(false); } }; fetchPins(); console.log(Fetching pins for text: ${text} and userId: ${userId}); }, [text, userId]); ~ i am working with sanity and i want to change the pins displayed when the text changes. problem is the text does change from created to save and runs the code as expected but when i change it back to created, it still runs the code with savedQuery not the createdQuery
12 Replies
glutonium
glutonium•6mo ago
send in codeblocks
useEffect(() => {
const fetchPins = async () => {
setLoading(true);
setError(null);

try {
let query;

if (text === "created") {
console.log("Running userCreatedPinsQuery");
query = userCreatedPinsQuery(userId);
} else {
console.log("Running userSavedPinsQuery");
query = userSavedPinsQuery(userId);
}

console.log("Generated query:", query);

const data = await client.fetch(query);

console.log("Fetched data:", data);

setPins(data);
} catch (err) {
console.error("Error fetching pins:", err);
setError(err);
} finally {
setLoading(false);
}
};

fetchPins();
console.log(Fetching pins for text: ${text} and userId: ${userId});
}, [text, userId]);
useEffect(() => {
const fetchPins = async () => {
setLoading(true);
setError(null);

try {
let query;

if (text === "created") {
console.log("Running userCreatedPinsQuery");
query = userCreatedPinsQuery(userId);
} else {
console.log("Running userSavedPinsQuery");
query = userSavedPinsQuery(userId);
}

console.log("Generated query:", query);

const data = await client.fetch(query);

console.log("Fetched data:", data);

setPins(data);
} catch (err) {
console.error("Error fetching pins:", err);
setError(err);
} finally {
setLoading(false);
}
};

fetchPins();
console.log(Fetching pins for text: ${text} and userId: ${userId});
}, [text, userId]);
Judy
JudyOP•6mo ago
You mean i should send it in this form. Or just pictures or how?
Jochem
Jochem•6mo ago
there's instructions on how to use codeblocks in #how-to-ask-good-questions
Judy
JudyOP•6mo ago
useEffect(() => {
const fetchPins = async () => {
setLoading(true);
setError(null);

try {
let query;

if (text === "created") {
console.log("Running userCreatedPinsQuery");
query = userCreatedPinsQuery(userId);
} else {
console.log("Running userSavedPinsQuery");
query = userSavedPinsQuery(userId);
}

console.log("Generated query:", query);

const data = await client.fetch(query);

console.log("Fetched data:", data);

setPins(data);
} catch (err) {
console.error("Error fetching pins:", err);
setError(err);
} finally {
setLoading(false);
}
};

fetchPins();
console.log(`Fetching pins for text: ${text} and userId: ${userId}`);
}, [text, userId]);
useEffect(() => {
const fetchPins = async () => {
setLoading(true);
setError(null);

try {
let query;

if (text === "created") {
console.log("Running userCreatedPinsQuery");
query = userCreatedPinsQuery(userId);
} else {
console.log("Running userSavedPinsQuery");
query = userSavedPinsQuery(userId);
}

console.log("Generated query:", query);

const data = await client.fetch(query);

console.log("Fetched data:", data);

setPins(data);
} catch (err) {
console.error("Error fetching pins:", err);
setError(err);
} finally {
setLoading(false);
}
};

fetchPins();
console.log(`Fetching pins for text: ${text} and userId: ${userId}`);
}, [text, userId]);
Jochem
Jochem•6mo ago
please don't send screenshots though
Judy
JudyOP•6mo ago
Is this better?
Jochem
Jochem•6mo ago
you can get syntax highlighting if you use ```js intead of just ```, but yes! no idea how to answer your question though, sorry
Judy
JudyOP•6mo ago
ok No problem. Atleast i learnt how to use code blocks😊
Aoi
Aoi•6mo ago
Can you send the rest of the code for the component? Also just try adding
console.log(text === "created", text)
console.log(text === "created", text)
Before the if statement. (Or you can use the web debugger if you know how to)
Judy
JudyOP•6mo ago
~~~
Judy
JudyOP•6mo ago




Judy
JudyOP•6mo ago
This is what the entire component looks like I found the problem. LOL silly me used an uppercase in the wrong place. 🤣🤣🤣🤣🤣🤣🤣🤣🤣
Want results from more Discord servers?
Add your server