Skip to content

Instantly share code, notes, and snippets.

@hijiangtao
Last active July 22, 2021 10:15
Show Gist options
  • Save hijiangtao/96fce8470e68c9b2062168710c45a734 to your computer and use it in GitHub Desktop.
Save hijiangtao/96fce8470e68c9b2062168710c45a734 to your computer and use it in GitHub Desktop.
React Switch Compnent
import { useState }from 'react'
function SwitchComponent() {
const Switch = props => {
const { key, children } = props
return children.find(child => {
return child.props.value === key
})
}
const [state, setState] = useState(true)
return (
<>
<Switch key={state}>
<div value={false}>Will display if state is false</div>
<div value={true}>Will display if state is true</div>
</Switch>
<button onClick={()=>setState(val=>!val)}>Change Switch Case Value</button>
</>
)
}
export default SwitchComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment