with-component-hooks
TypeScript icon, indicating that this package has built-in type declarations

1.1.4 • Public • Published

with-component-hooks

Use React hooks in class component

Install

npm install --save with-component-hooks

Part 1

Use WithComponentHooks as React.ElementType

interface Props {
  children: () => React.ReactNode;
}
import WithComponentHooks from "with-component-hooks";

export default class MyComponent extends React.Component {
  render() {
    return (
      <WithComponentHooks>
        {() => {
          const [counter, set] = React.useState(0);

          //TODO...

          return <div>{counter}</div>;
        }}
      </WithComponentHooks>
    );
  }
}

Part 2

Use hooks in render method

import WithComponentHooks from "with-component-hooks";

class MyComponent extends React.Component {
  render() {
    const [counter, set] = React.useState(0);

    //TODO...

    return <div>{counter}</div>;
  }
}

export default WithComponentHooks(MyComponent);

Use decorator

import WithComponentHooks from "with-component-hooks";

@WithComponentHooks
export default class MyComponent extends React.Component {
  render() {
    const [counter, set] = React.useState(0);

    //TODO...

    return <div>{counter}</div>;
  }
}

Package Sidebar

Install

npm i with-component-hooks

Weekly Downloads

2

Version

1.1.4

License

MIT

Unpacked Size

4.4 kB

Total Files

5

Last publish

Collaborators

  • bplok20010