Last active
July 22, 2021 10:15
-
-
Save hijiangtao/96fce8470e68c9b2062168710c45a734 to your computer and use it in GitHub Desktop.
React Switch Compnent
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
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