
Security News
Feross on the 10 Minutes or Less Podcast: Nobody Reads the Code
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.
hook-adapter
Advanced tools
A lightweight adapter pattern for using React hooks in class components.
Useful while transitioning from class components to functional components.
Allows you to wrap blocks of JSX within a class component with the result of a hook.
All hooks are supported, and all hook behaviors like re-renders, state updates, and side effects are preserved.
npm install hook-adapter
When the hook requires no parameters, you can simply pass the hook function itself to the hook prop of the HookAdapter component.
The result of the hook is then passed to the children of the HookAdapter component via a function children prop.
import React, { Component } from "react";
import { HookAdapter } from "hook-adapter";
import { useMyHook } from "./use-my-hook";
export class MyComponent extends Component {
render() {
return (
<div>
<HookAdapter hook={useMyHook}>
{hookResult => <AComponentThatNeedsTheHookResult data={hookResult} />}
</HookAdapter>
</div>
);
}
}
When the hook requires parameters, there are two approaches you can take:
Wrap the hook in a function that takes no parameters
import React, { Component } from "react";
import { HookAdapter } from "hook-adapter";
import { useMyHook } from "./use-my-hook";
export class MyComponent extends Component {
render() {
return (
<div>
<HookAdapter hook={() => useMyHook("param1", "param2")}>
{hookResult => <AComponentThatNeedsTheHookResult data={hookResult} />}
</HookAdapter>
</div>
);
}
}
Pass in the parameters to the HookAdapter component via the parameters prop
import React, { Component } from "react";
import { HookAdapter } from "hook-adapter";
import { useMyHook } from "./use-my-hook";
export class MyComponent extends Component {
render() {
return (
<div>
<HookAdapter hook={useMyHook} parameters={["param1", "param2"]}>
{hookResult => <AComponentThatNeedsTheHookResult data={hookResult} />}
</HookAdapter>
</div>
);
}
}
Both approaches are equivalent and are fully type-safe.
You may find that the latter approach works better with certain linting rules.
hook-adapter is licensed under the ISC license.
FAQs
A lightweight adapter pattern for using React hooks in class components.
We found that hook-adapter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.