Skip to content

Instantly share code, notes, and snippets.

@UsmanHaider15
Last active November 6, 2021 19:03
Show Gist options
  • Save UsmanHaider15/e9694bfb666a94d5bf26510aaff1f2ab to your computer and use it in GitHub Desktop.
Save UsmanHaider15/e9694bfb666a94d5bf26510aaff1f2ab to your computer and use it in GitHub Desktop.
import { useState } from "react";
const Switch = ({ test, children }) =>
children.find((child) => child.props.value === test);
function SwitchComponent() {
const [value, setValue] = useState("first");
return (
<div>
<Switch test={value}>
<div value="first">This is First Case</div>
<div value="second">This is Second Case</div>
</Switch>
<br />
<div>
<button onClick={() => setValue("first")}>First</button>
<button onClick={() => setValue("second")}>second</button>
</div>
</div>
);
}
export default SwitchComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment