Last active
December 1, 2019 14:13
-
-
Save danielsmykowski1/c2898c017cccb7dedd2b5879729615a7 to your computer and use it in GitHub Desktop.
A code sample for React Hooks
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, { useEffect, useState } from "react"; | |
function CustomersWithGender({ gender }) { | |
const [customers, setCustomers] = useState([]); | |
useEffect( | |
async () => { | |
const res = await fetch(`https://upoint.store.back.au/api/customers?gender=${gender}`); | |
const json = await res.json(); | |
setCustomers(json.results); | |
}, | |
[gender] | |
); | |
return ( | |
<div class="customer-list"> | |
{ customers.map(customer => ( | |
<div class="customer" key={customer.id}> | |
{`The person name is: ${customer.name.title} ${customer.name.first} ${customer.name.last}`} | |
</div> | |
} | |
</div> | |
); | |
} | |
export function CustomerListByGender() { | |
const [gender, setGender] = useState("female"); | |
return ( | |
<div> | |
<p>Select Gender</p> | |
<button onClick={() => setGender("male")}>Male</button> | |
<button onClick={() => setGender("female")}>Female</button> | |
<CustomersWithGender gender={gender} /> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment