Created
March 5, 2020 04:38
-
-
Save iswanj/75b408461b0ed1b1bde57dbdbcfe24c5 to your computer and use it in GitHub Desktop.
ustated next
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
import React from "react"; | |
import axios from "axios"; | |
function useUsers() { | |
const [userById, setUsersById] = React.useState({}); | |
const [userIds, setUsers] = React.useState([]); | |
const getUserList = async () => { | |
const result = axios.get("/api/users"); | |
setUsersById( | |
result.reduce((obj, item) => { | |
return { | |
...obj, | |
[item.id]: item | |
}; | |
}, {}) | |
); | |
setUsers(result.map(item => item.id)); | |
}; | |
return { getUserList, userById, userIds }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment