Skip to content

Instantly share code, notes, and snippets.

@thewisenerd
Created February 15, 2023 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thewisenerd/d6b3349ffd1fe861a811aa174f768a18 to your computer and use it in GitHub Desktop.
Save thewisenerd/d6b3349ffd1fe861a811aa174f768a18 to your computer and use it in GitHub Desktop.
interface ExtractHookProps<T> {
extractor: () => T;
setup: (hookValue: T) => void;
}
function ExtractHook<T>(props: React.PropsWithChildren<ExtractHookProps<T>>) {
let {extractor, setup, ...others} = props;
let hookValue = extractor();
setup(hookValue);
return <div {...others} />
}
@thewisenerd
Copy link
Author

class Component2 extends React.Component<{}, {}> {
    render() {
        let locationTuple: LocationTuple;
        return <div>
            <ExtractHook extractor={() => useLocation()} setup={(r) => {
                locationTuple = r
            }}/>
            Hello
            <br/>
            <button onClick={() => {
                const [_, setLocation] = locationTuple;
                setLocation("/users/abcd");
                console.log("wat btn")
            }}>html
            </button>
        </div>;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment