Giter Club home page Giter Club logo

thinjector's Introduction

Thinjector (React)

Minimalistic, super lightweight React service injection container extremely easy to use.

Why

It's important to decouple your ui components from your services (fetching logic, biz logic, etc..) but the solutions out there are either using heavy weight state management tools (redux + redux saga, mobx, recoil, etc..) or full fledge dependency containers (inversifyjs, tsyringe) which for a lot of use cases are overkill so looking for lightweight solutions the ones I found which are awesome but didn't feel comfortable with the api, so I came with this very small solution.

Features

  • Built with Typescript.
  • A hook for accessing services.

Installation

npm install thinjector

Usage

Set up your service container, create the file where you wish in your project folder structure, I will put it on services/index.ts.

import { createServiceContainer } from 'thinjector'

// Service structure is up to you, this is just a simple example
interface UserService {
    login: VoidFunction
}
export interface IServices {
    userService: UserService
}

const services: IServices = {
    userService: {
        login: () => console.log('signing in....'),
    }
}

export const container =
  createServiceContainer<IServices>(services);

And ... that's it !, you can start accessing your services on your react components.

Injecting services examples

Using useService hook !

import React from "react"
import container from "./services"

const { useService } = container;

const DemoPage = () => {
  const { userService } = useService(); // WoW, just like that
  return (
    <div onClick={() => userService.login()}>
      Demo Page
    </div>
  );
};

export default DemoPage;

Not a hook fan ? still using class components ?, don't worry, we got you covered with a HOC too !

Using withService HOC !

import React from "react"
import container, {IServices} from "./services"

const { withService } = container;

type Props = {
    service: IServices
}
const DemoPage = ({service}: Props) => {
  return (
    <div onClick={() => service.userService.login()}>
      Demo Page
    </div>
  );
};

export default withService(DemoPage);

Don't like the idea of injecting all services and prefer a solution to specify which services or functions you want like a redux mapToProps thing ? say no more:

Using inject HOC !

import React from "react"
import container, {IServices} from "./services"

const { inject } = container;

type Props = {
    login: IServices['userService']['login']
}
const DemoPage = ({login}: Props) => {
  return (
    <div onClick={() => login()}>
      Demo Page
    </div>
  );
};

export default inject(DemoPage, (services) => ({
    login: services.userService.login
}));

TODO :

  • Accessing services from within services.
  • Make agnostic for any js framework or library.

thinjector's People

Contributors

cesarj41 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.