ABC component renders multiple time ( 4 times).
i am using custom hooks. In custom hooks all we are doing is using “useMutation” from apollo client and calling the “mutation” function provide by “useMutation” inside useeffect.
Problem: As per my knowledge react renders initially and in each subsequent call of setting state value.
So why is it not rendering only 2 times… why is it rendering 4 times?
am i missing something, Is there some return statement i can call for “useMutation” ??
my code:
import useStripeInitializePublishableKey from "../hooks/useStripeInitializePublishableKey";
const ABC = () => {
console.log("ABC :: loading....");
const [paymentGatewayPubKey, pubKeyLoading, pubKeyError] =
useStripeInitializePublishableKey();
return (
<>
<Text>hello world</Text>
</>
);
};
export default ABC;
file: useStripeInitializePublishableKey.js
import { useEffect, useState } from "react";
import { useMutation } from "@apollo/client";
import { GET_PAYMENT_GATEWAY_PUB_KEY } from "@api/graphql_api";
const useStripeInitializePublishableKey = () => {
const [paymentGatewayPubKey, setPaymentGatewayPubKey] = useState("");
const [
getPaymentGatewayPubkey,
{ data: pubKeyData, loading: pubKeyLoading, error: pubKeyError },
] = useMutation(GET_PAYMENT_GATEWAY_PUB_KEY);
useEffect(() => {
getPaymentGatewayPubkey({
variables: {
paymentMethodTypes: ["card"],
},
});
}, []);
useEffect(() => {
if (pubKeyData) {
setPaymentGatewayPubKey(
pubKeyData.get_stripe_publishable_key.publishable_key
);
}
}, [pubKeyData]);
return [paymentGatewayPubKey, pubKeyLoading, pubKeyError];
};
export default useStripeInitializePublishableKey;