Home > Blockchain >  Why does my child component render multiple times?
Why does my child component render multiple times?

Time:05-16

I am using Stripe API on my project and when the payment is successful I want to add the order placed to my collection. When I run the below code it adds the order multiple times and there is no specific pattern. It also adds multiple orders to the collection:

Success.js:

import React from "react";
import AddOrder from "../orders/AddOrder";
import { useNavigate, useParams } from "react-router-dom";
import { useMutation, useQuery } from "@apollo/client";
import queries from "../../queries";
import { Alert, Button } from "react-bootstrap";

function Success() {
    const logo = require("../../assets/delivery-package.gif");
    const navigate = useNavigate();
    const { secret } = useParams();
    const { loading, error, data } = useQuery(queries.GET_SESSION, { variables: { id: secret } });
    const [deleteSession] = useMutation(queries.DELETE_SESSION);
    if (loading) {
        return <div>Loading...</div>;
    } else if (error) {
        navigate("/notfound");
    } else if (data.session) {
        deleteSession({
            variables: {
                id: secret,
            },
        });
        return (
            <div>
                <AddOrder />

                <Alert variant="success" style={{ fontSize: "25px" }}>
                    Order Placed Successfully
                </Alert>
                <img alt="order success" id="logo" src={logo} style={{ width: "70%", height: "70%", marginTop: "30px" }} />
                <br />
                <br />
                <Button onClick={() => navigate("/")}>Home</Button>
            </div>
        );
    } else {
        navigate("/notfound");
    }
}

export default Success;

AddOrder:

    import { useMutation, useQuery } from "@apollo/client";
import { AuthContext } from "../../Firebase/Auth";
import queries from "../../queries";
import { useContext, useEffect } from "react";

import { reactLocalStorage } from "reactjs-localstorage";
let add = reactLocalStorage.getObject("addressDetails");

function AddOrder() {
    const d = new Date();
    let text = d.toString();
    const [addOrder] = useMutation(queries.ADD_ORDER);
    const [editUser] = useMutation(queries.EDIT_USER_CART);

    const { currentUser } = useContext(AuthContext);

    const { data } = useQuery(queries.GET_USER_BY_ID, {
        fetchPolicy: "network-only",
        variables: {
            id: currentUser.uid,
        },
    });

    const getUserOrders = useQuery(queries.GET_USER_ORDERS, {
        fetchPolicy: "network-only",
        variables: {
            userId: currentUser.uid,
        },
    });
    useEffect(() => {
        if (data && getUserOrders.data && currentUser && data.getUser.cart.length > 0) {
            let newCart = [];
            let total = 0;
            for (let i = 0; i < data.getUser.cart.length; i  ) {
                total  = data.getUser.cart[i].price * data.getUser.cart[i].quantity;
                newCart.push({
                    orderedQuantity: data.getUser.cart[i].quantity,
                    _id: data.getUser.cart[i]._id,
                    name: data.getUser.cart[i].name,
                    image: data.getUser.cart[i].image,
                    price: data.getUser.cart[i].price,
                });
            }
            addOrder({
                variables: {
                    userId: currentUser.uid,
                    userEmail: currentUser.email,
                    status: "ordered",
                    createdAt: text,
                    products: newCart,
                    total: total,
                    flag: getUserOrders.data.userOrders.length   1,
                    zip: add.zip.val ? add.zip.val : add.zip,
                    state: add.state.val ? add.state.val : add.state,
                    city: add.city.val ? add.city.val : add.city,
                    apt: add.apt.val ? add.apt.val : add.apt,
                    addressStreet: add.addressStreet.val ? add.addressStreet.val : add.addressStreet,
                },
            });

            editUser({
                variables: {
                    id: currentUser.uid,
                    cart: [],
                },
            });
        }
    }, [addOrder, currentUser, data, editUser, getUserOrders, text]);
}
export default AddOrder;

There is only one return so don't know if the issue is with the success page or the Addorder function

CodePudding user response:

In AddOrder component, you are not returning any JSX. You are actually sending order request in the body of the component ( during rendering, which is super bad since it is a side-effect).

Don't return addOrder, editUser calls. Place them inside of useEffect or event handlers depending on your business logic.

CodePudding user response:

Possible reason:

Your component must be being rendered by some parent component, for example, by calls to setState, and when the component is re-rendered the Success child component (which in turn does not return a JSX.Element) is also re-rendered

Cause:

Inside your AddOrder component there is a useEffect that serves to prevent re-renders when the passed parameters were not changed, and among them it has a "new Date().getTime()", I believe this is one of the main reasons why useEffect is always returning true, and requesting a re-render. Not only that, but also:

  1. new Date().toString as parameter to useEffect

  2. Passing functions (addOrder, editOrder) to useEffect params that are likely to always be new functions when reredendering.

  3. currentUser is probably a new object every render, luckily you only need the userid, which in turn is a primitive type, and will always be the same regardless of re-rendering.

Solution:

There is no need to analyze if the addOrder or editOrder function has changed, as they are just functions, and do not add any specific "value", for example, if your intention is to rederize (to be able to add) only when the items change, you can just leave the getUserOrders.data and data (useQuery(queries.GET_USER_BY_ID)...)

New code:

import { useMutation, useQuery } from "@apollo/client";
import { AuthContext } from "../../Firebase/Auth";
import queries from "../../queries";
import { useContext, useEffect } from "react";

import { reactLocalStorage } from "reactjs-localstorage";
let add = reactLocalStorage.getObject("addressDetails");

function AddOrder() {
    const d = new Date();
    let text = d.toString();
    const [addOrder] = useMutation(queries.ADD_ORDER);
    const [editUser] = useMutation(queries.EDIT_USER_CART);

    const { currentUser } = useContext(AuthContext);

    const { data } = useQuery(queries.GET_USER_BY_ID, {
        fetchPolicy: "network-only",
        variables: {
            id: currentUser.uid,
        },
    });

    const getUserOrders = useQuery(queries.GET_USER_ORDERS, {
        fetchPolicy: "network-only",
        variables: {
            userId: currentUser.uid,
        },
    });
    useEffect(() => {
        if (data && getUserOrders.data && currentUser && data.getUser.cart.length > 0) {
            let newCart = [];
            let total = 0;
            for (let i = 0; i < data.getUser.cart.length; i  ) {
                total  = data.getUser.cart[i].price * data.getUser.cart[i].quantity;
                newCart.push({
                    orderedQuantity: data.getUser.cart[i].quantity,
                    _id: data.getUser.cart[i]._id,
                    name: data.getUser.cart[i].name,
                    image: data.getUser.cart[i].image,
                    price: data.getUser.cart[i].price,
                });
            }
            addOrder({
                variables: {
                    userId: currentUser.uid,
                    userEmail: currentUser.email,
                    status: "ordered",
                    createdAt: text,
                    products: newCart,
                    total: total,
                    flag: getUserOrders.data.userOrders.length   1,
                    zip: add.zip.val ? add.zip.val : add.zip,
                    state: add.state.val ? add.state.val : add.state,
                    city: add.city.val ? add.city.val : add.city,
                    apt: add.apt.val ? add.apt.val : add.apt,
                    addressStreet: add.addressStreet.val ? add.addressStreet.val : add.addressStreet,
                },
            });

            editUser({
                variables: {
                    id: currentUser.uid,
                    cart: [],
                },
            });
        }
    }, [currentUser.uid, data, getUserOrders.data]);
}
export default AddOrder;

Please check if the code works, anything please comment so I can correct my answer (I haven't tested it yet, but I believe it will work).


Although it is not recommended, as the good practice recommends the use of memos, you can try in the last case (only to verify if this is in fact the problem) to use as follows:

[currentUser.uid, JSON.stringify(data), JSON.stringify(getUserOrders.data)]

  • Related