Skip to content

Instantly share code, notes, and snippets.

@phobon
Created September 29, 2020 05:53
Show Gist options
  • Save phobon/a18e2a510596c7dceb5152ac509c2c41 to your computer and use it in GitHub Desktop.
Save phobon/a18e2a510596c7dceb5152ac509c2c41 to your computer and use it in GitHub Desktop.
Consuming data
function fetchLedBrands = async () => {
// This can be sourced from anything, an api endpoint etc.
//[
// { id: 1, label: "a" },
// { id: 2, label: "b" },
// { id: 3, label: "c" },
// { id: 4, label: "d" },
// { id: 5, label: "e" },
//];
const ledBrands = await fetch("...");
return ledBrands;
};
function fetchDesignSpecs = async () => {
// [
// { id: 1, ledBrand: { id: 2, label: "b" } }.
// { id: 2, ledBrand: { id: 3, label: "c" } }.
// { id: 3, ledBrand: { id: 2, label: "b" } }.
// ]
const designSpecs = await fetch("...");
return designSpecs;
}
const App = () => {
const [brands, setBrands] = useState(() => []);
const [specs, setSpecs] = useState(() => []);
useEffect(async () => {
const b = await fetchLedBrands();
setBrands(b);
}, []);
useEffect(async () => {
const s = await fetchDesignSpecs();
setSpecs(s);
}, []);
return (
<>
// Do something with your specs here
<select>
{brands.map(({ id, label }) => (
<option key={id} value={id}>{label}</option>
)}
</select>
</>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment