Skip to content

JonhnyDev/react-awilix

 
 

Repository files navigation

React Awilix

npm version CI Coverage Status license: Apache-2.0

React.Context wrapper that passes AwilixContainer to components.

Installation

npm install react-awilix

or

yarn add react-awilix

Requirements

  • react >=16.8.0 || >=17.0.0 or preact >=10.0.0
  • awilix >=4.2.5 (awilix requires several es6 objects such as Proxy, Reflect, and Symbol)

Usage

import React from 'react';
import { createContainer, asClass } from 'awilix';
import { ContainerProvider, useCradle } from 'react-awilix';

import { ServiceClass } from './services';

const container = createContainer<{ service: ServiceClass }>();
container.register('service', asClass(ServiceClass).singleton());

function Button({ children }: { children: string }) {
  const { service } = useCradle<{ service: ServiceClass }>();
  return <button onClick={() => { service.runAction(); }}>{children}</button>;
}

ReactDOM.render(
  <ContainerProvider container={container}>
    <Button>run</Button>
  </ContainerProvider>,
  document.getElementById('root')
);

License

React Awilix is licensed under the Apache 2.0 License

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 78.9%
  • JavaScript 21.1%