Skip to content

Instantly share code, notes, and snippets.

@AndresdoSantos
Last active August 20, 2023 02:08
Show Gist options
  • Save AndresdoSantos/763c2bf57618291a1d01dbcfdd0298c3 to your computer and use it in GitHub Desktop.
Save AndresdoSantos/763c2bf57618291a1d01dbcfdd0298c3 to your computer and use it in GitHub Desktop.
This gist shows how to make your JSX cleaner using objects.
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