1

I'm very new to react. There is a place where I'm stuck.

I have action buttons. When I press one, firstly it is checked whether it has been liked before, and then I use useMutate to remove the like if it is liked, and to create the like if it is not.

Let's say I pressed a button and then I pressed another button. The selectedId variable and the result of the query I made in useQuery still show the old query value in the incoming variable, both of them change much later.

The query in the code was like this:

 //QUERY
    const scenario_target_type: number = 5;
    const filterParam = `${scenario_target_type}/${selectedScenarioId}`;
    const {
        data: scenariolikes,
        isLoading: isLoadingLikeScenarios,
        refetch: refetch1,
    } = useQuery(
        ["likes_scenarios",selectedScenarioId ],
        async () => {
            return await (await csrCommentApi.get(`/likes/${filterParam}/check`, {
                headers: {
                    "Authorization": `Bearer ${getToken()}`
                }

            })).data?.message;
        },
        {
            enabled: !!selectedScenarioId,
            keepPreviousData: false,
            refetchOnWindowFocus: true,
            refetchOnMount: true,
            refetchOnReconnect: true,
        }


    );

and the code for the ActionButton is as below ActionButton Code

Also the console output Console output

As you can see, the console shows the value 10037 that I pressed before, but 10033, which I just pressed, was sent to mutate. I would be very happy if you help. Thank you

When I pressed the button, I wanted the relevant scenario to be selected, immediately querying that scenario and entering if and else blocks according to that query.

deepanshu223
  • 433
  • 2
  • 6

0 Answers0