Last active
August 20, 2023 02:08
-
-
Save AndresdoSantos/763c2bf57618291a1d01dbcfdd0298c3 to your computer and use it in GitHub Desktop.
This gist shows how to make your JSX cleaner using objects.
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
function ZeroComponent() { | |
return <p>0</p> | |
} | |
function OneComponent() { | |
return <p>1</p> | |
} | |
function TwoComponent() { | |
return <p>2</p> | |
} | |
const COMPONENTS = { | |
0: <ZeroComponent />, | |
1: <OneComponent />, | |
2: <TwoComponent /> | |
} | |
export function MyComponent() { | |
const [index, setIndex] = useState(0) | |
return { | |
<> | |
{COMPONENTS[index]} | |
<button onClick={() => setIndex((prev) => prev + 1)}>Next component</button> | |
</> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment