Created
March 5, 2020 04:38
-
-
Save iswanj/c4a72a1c8fd79e5646c9beedca03970d 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