user10625391
user10625391

Reputation: 317

how to load a function when a page load react native

I'm using react-native with hooks, and I'm trying to load a JSON from AsyncStorage every time a user opens one of my react-native screens This JSON contains information on what my states should be set to.

How can I call a function that runs every time this screen is opened?

i know that without hooks this should be done with useEffect, but when i put my api call there it makes an error

this is my code

useEffect(() => {
const getKind = () => {
    ForceApi.post(`/GetKindPensionController.php`)
      .then(res => {
        setpPensionKind(res.data.pension);
    })
  }


}, []);

Upvotes: 2

Views: 6777

Answers (1)

Fábio BC Souza
Fábio BC Souza

Reputation: 1220

You are missing call the getKind, and it should be a async function! For a better code try something like:

useEffect(() => {
    async function getKind() {
       const { data } = await ForceApi.post(`/GetKindPensionController.php`)
       setpPensionKind(data.pension);
    }

    getKind();
}, []);

Upvotes: 4

Related Questions