Been pulling my hair over this one the whole day and I feel like I've tried pretty much everything, even a dozen StackOverflow answers, to no avail...
tldr; given a specific trigger which happens externally (and just fine) I'm trying to get data from Firestore and update local state (an array) with this new data. Now I know that for React to re-render when updating an array, it will disregard the contents and considers the reference to that array only. The problem is, React seems to not trigger a re-render even when setting the state to a completely new array.
Here is the code:
const Recipe = (irrelevantProps) => {
// A few other state hooks here, not shown for clarity's sake
const [ingredients, setIngredients] = useState<IngredientType[]>([]);
useEffect(() => {
const getIngredientsDocs = () => {
return ingredientsRefs.map(async (ingredientRef) => {
const docRef = doc(firestore, "ingredients", ingredientRef.id);
const docData = (await getDoc(docRef)).data() as IngredientType;
return docData;
});
};
Promise.all(getIngredientsDocs()).then((newIngredients) => {
// I would assume updating the state with a new array would trigger a re-render, but it doesn't
setIngredients(newIngredients);
});
}, [externalTrigger]);
For what it's worth, I'm pretty sure everything else is working as expected.
Any hints, suggestions, tips or downright solutions will be VERY much appreciated.
To whomever may read this, thanks a ton regardless!