Created
June 1, 2019 07:57
-
-
Save ivan-ha/82f00a46400071a14edb6aedd49e3bea to your computer and use it in GitHub Desktop.
react-hooks-test-custom-hooks
This file contains hidden or 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
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> | |
); | |
}; |
This file contains hidden or 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
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