Skip to content

Instantly share code, notes, and snippets.

@joeljuca
Last active October 2, 2020 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeljuca/e5854d58669fefe702e2e57bc27e136d to your computer and use it in GitHub Desktop.
Save joeljuca/e5854d58669fefe702e2e57bc27e136d to your computer and use it in GitHub Desktop.
const FruitList = (() => {
// Aqui, PRODUCT_LABELS passa a existir apenas dentro desse bloco,
// o que na prática funciona igual a uma closure.
const PRODUCT_LABELS = {
456: { single: "Apple", plural: "Apples" },
123: { single: "Banana", plural: "Bananas" },
789: { single: "Oranges", plural: "Orangess" },
};
// O fato do nome interno ser o mesmo do nome externo é apenas para
// fácil identificação. Isso poderia ser FruitListComponent, etc.
const FruitList = ({ fruits }) => {
return (
<ul>
{fruits
.filter((fruit) => fruit.quantity > 0)
.map((fruit) => (
<li>
{fruit.quantity === 1
? PRODUCT_LABELS[fruit.id].single
: PRODUCT_LABELS[fruit.id].plural}
</li>
))}
</ul>
);
}
return FruitList;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment