const { data, error, isLoading } = useMoralisQuery("Products", (query) =>
query.equalTo("objectId", objectId)
);
if (error) {
return <span>🤯</span>;
}
if (isLoading) {
return <span>🙄</span>;
}
let json = JSON.stringify(data, null, 2);
const product: ProductClass = JSON.parse(json)[0];
return <TransferButton product={product} />;
};
const TransferButton = ({ product }: { product: ProductClass }) => {
const { user } = useMoralis();
const { fetch, error, isFetching } = useWeb3Transfer({
amount:
product !== undefined
? Moralis.Units.ETH(product.price)
: Moralis.Units.ETH(0),
receiver: product !== undefined ? product.user.managed_account_pub : "0x0",
type: "native",
});
const [fetched, setFetched] = useState(false);
const [called, setcalled] = useState(false);
const { isSaving, save } = useNewMoralisObject("Subscription");
let x = undefined;
let counter = 0;
if (called) {
if (!fetched) {
setFetched(true);
if (product !== undefined) {
if (product.recurrence !== "One time") {
if (x == undefined) {
x = null;
console.log("hello");
let y = fetch();
x = save({ product: product.objectId, status: true, user: user });
console.log("x: ", x);
console.log("y: ", y);
counter = counter + 1;
console.log("counter: ", counter);
}
}
}
}
}
return (
<div>
{error && <h1>Error: {error}</h1>}
<button
disabled={isFetching}
onClick={() => {
setcalled(true);
}}
className="h-7 text-sm bg-primary rounded-sm text-black font-display px-2 flex justify-center items-center cursor-pointer"
>
Transfer
</button>
</div>
);
}; ```
> The console.log('hello') and other console logs only execute once, but the fetch() and the save() works twice
Its very hard to understand what you are doing. So when someone clicks that button you switch the state called to setcalled(true) and then inside your component you have a if(called) then fetch.
You should put all logic whenever the state switches into useEffect
useEffect(() => {
if(called) {
// doLogic()
}
}, [called])
reply to
Its very hard to understand what you are doing
Code is law.