Created
March 12, 2022 17:37
-
-
Save flexseth/978b5f90be71ac4a75be9fb3f4286a07 to your computer and use it in GitHub Desktop.
useEffect WordPress API Fetch hook - initialize data from Users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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