Skip to content

Instantly share code, notes, and snippets.

@iswanj
Created March 5, 2020 04:38
Show Gist options
  • Save iswanj/c4a72a1c8fd79e5646c9beedca03970d to your computer and use it in GitHub Desktop.
Save iswanj/c4a72a1c8fd79e5646c9beedca03970d to your computer and use it in GitHub Desktop.
ustated next
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