score:1

Accepted answer

you can just call the searchusers() function in useeffect() to get some users and set the state.

if you want to get initial users with some other logic you should probably write a different function and then call it when setting up the component.

const getinitialusers = async () => {
    setloading();
    let text = "blabla"; // or whatever your initial user query should look like modify the url below accordingly
    const res = await axios.get(
      `https://api.github.com/search/users?q=${text}&client_id=${githubclientid}&client_secret=${githubclientasecret}`
    );

    //dispatch to reducer object
    dispatch({
      type: search_users,
      payload: res.data.items,
    });
  };

useeffect(()=>{
  getinitialusers();
},[]);

score:0

the reasonable and suggest place to get your first async data is componentdidmount which in hook world it is translated to use effect with an empty array as its dependencies

const [state, dispatch] = usereducer(githubreducer, initialstate);

useeffect(() => {
  searchusers('initaltextsearchfrompropsoranywhere')
}, [])

for more enhancement, you can call .then to show snack bar and .catch on to retry in case it fails for some reason and .finally to set loading to false in both cases.


Related Query

More Query from same tag