Skip to content

Instantly share code, notes, and snippets.

@jkappers
Created February 28, 2019 19:34
Show Gist options
  • Save jkappers/ee6e46a9ff4b97f6d705b8db508729e9 to your computer and use it in GitHub Desktop.
Save jkappers/ee6e46a9ff4b97f6d705b8db508729e9 to your computer and use it in GitHub Desktop.
Using a hook for reusable Dialog state
import { useState } from 'react';
function DialogContext({ initialState = false, children }) {
const [isOpen, setIsOpen] = useState(initialState);
return children(
isOpen,
() => setIsOpen(true),
() => setIsOpen(false)
)
}
export default DialogContext;
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