Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WrapperComponent for React Hooks #316

Open
jmattheis opened this issue Mar 22, 2019 · 0 comments
Open

WrapperComponent for React Hooks #316

jmattheis opened this issue Mar 22, 2019 · 0 comments

Comments

@jmattheis
Copy link

The current recommendation for functional components is adding a static handler function. This solution doesn't work when the functional component is rendered multiple times.

It should be possible to add a compontent which accepts a callback that is called every time the outside was clicked.

import * as React from "react";
import onClickOutside from "react-onclickoutside";

interface OnClickOutsideProps {
    onClickOutside: (event) => void;
}

export class XOutsideClickHandler extends React.Component<OnClickOutsideProps>{
    public handleClickOutside = (event) => this.props.onClickOutside(event);
    public render = () => this.props.children;
}

export const OutsideClickHandler: React.ComponentClass<OnClickOutsideProps> = onClickOutside(XOutsideClickHandler);

It could be used with React hooks like this:

export const Menu = () => {
    const [open, setOpen] = React.useState(false);
    return (
        <OutsideClickHandler onClickOutside={() => setOpen(false)}>
            <input type="button" onClick={() => setOpen(true)}/>
            {`open = ${open}`}
        </OutsideClickHandler>
    );
};

Could this be added to this repository?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant