Skip to content

Instantly share code, notes, and snippets.

@flexseth
Created March 12, 2022 17:37
Show Gist options
  • Save flexseth/978b5f90be71ac4a75be9fb3f4286a07 to your computer and use it in GitHub Desktop.
Save flexseth/978b5f90be71ac4a75be9fb3f4286a07 to your computer and use it in GitHub Desktop.
useEffect WordPress API Fetch hook - initialize data from Users
// Note: the empty deps array [] means
// this useEffect will run once
// similar to componentDidMount()
useEffect(() => {
apiFetch( { path: '/wp/v2/users?roles=author,editor,administrator' } )
.then (
( users ) => {
setIsLoaded(true);
setUsers(users)
console.log(users)
},
// Note: it's important to handle errors here
// instead of a catch() block so that we don't swallow
// exceptions from actual bugs in components.
(error) => {
setIsLoaded(true)
setError(error)
}
);
}, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment