Skip to content

Instantly share code, notes, and snippets.

@ivan-ha
Created June 1, 2019 07:57
Show Gist options
  • Save ivan-ha/82f00a46400071a14edb6aedd49e3bea to your computer and use it in GitHub Desktop.
Save ivan-ha/82f00a46400071a14edb6aedd49e3bea to your computer and use it in GitHub Desktop.
react-hooks-test-custom-hooks
const App = () => {
const users = useRandomUsers();
return (
<div className="App">
<header className="App-header">
<ol>
{users &&
users.map(({ name, login }) => (
<li key={login.uuid}>
{`${name.title} ${name.first} ${name.last}`}
</li>
))}
</ol>
</header>
</div>
);
};
export const useRandomUsers = () => {
const [users, setUsers] = useState([]);
useEffect(() => {
(async () => {
try {
const response = await axios(
"https://randomuser.me/api/?results=10&inc=name,login&nat=us"
);
setUsers(response.data.results);
} catch (error) {
console.error(error);
}
})();
}, []);
return users;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment