Skip to content

Instantly share code, notes, and snippets.

@danielsmykowski1
Last active December 1, 2019 14:13
Show Gist options
  • Save danielsmykowski1/c2898c017cccb7dedd2b5879729615a7 to your computer and use it in GitHub Desktop.
Save danielsmykowski1/c2898c017cccb7dedd2b5879729615a7 to your computer and use it in GitHub Desktop.
A code sample for React Hooks
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