L
L
lookingfor22020-10-22 13:17:18
React
lookingfor2, 2020-10-22 13:17:18

Is it possible to pass a function to a hook state?

Got a hook

const [objectData, setObjectData] = useState(GetLoan);

GetLoan is a function that calls the API
export async function GetLoan() {
    return axios.get("/account/get-..../").then(res => res.data);
}

1) Is it possible to do this, pass a function to a hook?
2) Is async needed in the function here? axios and so returns a promise, I understand, but still interesting

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
abberati, 2020-10-22
@lookingfor2

The short answer is no, you can't.
Long answer in documentation

K
Kirill Makarov, 2020-10-23
@kirbi1996

You can’t do this, why not pass an empty object to useState , write a function below, the results of which will be setObjectData , and even below useEffect where add [] to the dependency

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question