Skip to content

Instantly share code, notes, and snippets.

@evasconcelos
Created March 30, 2021 08:01
Show Gist options
  • Save evasconcelos/dd7297f3ec70702309ab220b6eb0bbef to your computer and use it in GitHub Desktop.
Save evasconcelos/dd7297f3ec70702309ab220b6eb0bbef to your computer and use it in GitHub Desktop.
LSP Example
interface CatFact {
facts: string[];
color: string;
}
const CatFactA = ({ facts, color }: CatFact) => {
return (
<>
{facts.map((fact, index) => (
<p style={{ color }}>
Fact {index}: {fact}
</p>
))}
</>
);
};
const CatFactB = ({ facts, color }: CatFact) => {
return (
<ul style={{ color }}>
{facts.map((fact) => (
<li>{fact}</li>
))}
</ul>
);
};
export default () => {
const catFactData: CatFact = {
facts: [
"Cats make about 100 different sounds. Dogs make only about 10.",
"I don't know anything about cats.",
"Domestic cats spend about 70 percent of the day sleeping and 15 percent of the day grooming."
],
color: "red"
};
const abTest = Math.floor(Math.random() * 2) + 1;
return (
<div>
{abTest === 1 ? (
<CatFactA {...catFactData} />
) : (
<CatFactB {...catFactData} />
)}
</div>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment