Created
February 28, 2019 19:34
-
-
Save jkappers/ee6e46a9ff4b97f6d705b8db508729e9 to your computer and use it in GitHub Desktop.
Using a hook for reusable Dialog state
This file contains hidden or 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 DialogContext({ initialState = false, children }) { | |
const [isOpen, setIsOpen] = useState(initialState); | |
return children( | |
isOpen, | |
() => setIsOpen(true), | |
() => setIsOpen(false) | |
) | |
} | |
export default DialogContext; |
This file contains hidden or 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
const ChoiceView = () => ( | |
<DialogContext> | |
{(isOpen, handleOpen, handleClose) => ( | |
<Fragment> | |
<Fab | |
color="primary" | |
aria-label="Add" | |
onClick={handleOpen} | |
className={this.props.classes.fab}> | |
<Icon>add_icon</Icon> | |
</Fab> | |
<AddChoiceDialog open={isOpen} onClose={handleClose} /> | |
</Fragment> | |
)} | |
</DialogContext> | |
); | |
export default ChoiceView; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment