@kdevsoft/meta-injector-react
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

meta injector react logo

React binding for @kdevsoft/meta-injector


Installation

npm install @kdevsoft/meta-injector-react

Usage

Create application level hook and ReactElement for using @kdevsoft/meta-injector

// file: services.ts
import { injector } from './injector';

export const services = {
  service1: injector.createMeta<'str1'>(),
  service2: injector.createMeta<'str2'>(),
  service3: injector.createMeta<'str3'>(),
} as const;
// file: useInjector.ts
import { createMetaInjectorHook } from '@kdevsoft/meta-injector-react';
import { injector } from './injector';

export const useInjector = createMetaInjectorHook(injector);
// file: InjectorElement.ts
import { createMetaInjectorElement } from '@kdevsoft/meta-injector-react';
import { injector } from './injector';

export const InjectorElement = createMetaInjectorElement(injector);

Hook use case. Under the hood uses React.useMemo to reduce amount of render

// file: TestElement1.tsx
import { FC } from 'react';
import { services } from './services';
import { useInjector } from './useInjector';

const Element: FC = () => {
  const [service1, service2, service3] = useInjector(services.service1, services.service2, services.service3);

  return (
    <>
      <div>{service1 /* str1 */}</div>
      <div>{service2 /* str2 */}</div>
      <div>{service3 /* str3 */}</div>
    </>
  );
};

const TestElement1 = Element;
export default TestElement1;

Element use case. Under the hood uses useInjector hook with React.memo HOC with custom comparator

// file: TestElement2.tsx
import { FC } from 'react';
import { services } from './services';
import { InjectElement } from './InjectElement';

const Element: FC = () => (
  <InjectElement metaList={[services.service1, services.service2, services.service3]}>
    {(service1, service2, service3) => (
      <>
        <div>{service1 /* str1 */}</div>
        <div>{service2 /* str2 */}</div>
        <div>{service3 /* str3 */}</div>
      </>
    )}
  </InjectElement>
);

Do you like the package? Buy me a coffee :)

Buy Me A Coffee

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i @kdevsoft/meta-injector-react

      Weekly Downloads

      0

      Version

      1.0.0

      License

      MIT

      Unpacked Size

      4.41 kB

      Total Files

      6

      Last publish

      Collaborators

      • konstantin.kai