How can I implement the fetch implemented in the recoil selector to be executed when clicked?

Viewed 99

I want to use a selector to execute the fetch implemented in the selector at a time I want. (click for example)

export const phoneRequestSelector = selector({
key : "request",
get : async ({get}) => {
    const url = `${API_PATH}/goods`;

    const phoneRequestInfo:PhoneRequestInfo = {
        userName : get(nameState),
        phoneNo : get(mobNoState),
    }

    const result = await fetch(url, {
        method: 'POST',
        body: JSON.stringify(phoneRequestInfo),
        headers: {
            'Content-Type': 'application/json',
        },
    }).then(response => {
        return response;
    })

    return result;
}})

const requestData = useRecoilValueLoadable(phoneRequestSelector); 

const onClickHandler = () => { //where I want it to run

I would be very grateful if you could tell me what changes I need to make in order to run the above code only when I want it.

0 Answers
Related