npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

inversify-react-mvvm

v0.0.4

Published

Lightweight and opinionated mvvm glue/helper code for React/Inversify.

Downloads

17

Readme

Lightweight and opinionated mvvm glue/helper code for React/Inversify.

Motivation

For projects that embrace a MVVM pattern you usually end up writing some glue code to connect your React Components with the designated ViewModel.

This micro-library helps to reduce the aforementioned glue code by trying to make it as easy as possible to connect the two worlds.

Dependencies

This library is using inversify as a way to resolve/instantiate the view models, making it a hard dependency. If that is not your taste, feel free to just take the parts you like 😉

Installation

yarn add inversify-react-mvvm
# or
npm install inversify-react-mvvm

Usage

The ViewModel

All view models have to extend ViewModel. You don't have to implement any of the following methods though, these are for demonstration purposes.

provideByClass is an extension to provide from inversify-inject-decorators allowing you to register the decorated class by class reference in inversify. It is not a requirement to do it like that, you can also register your view models explicitely in your container.

You can use provideByClass not only for ViewModels, but also for your services/etc.

⚠️ provideByClass registeres the view models as Singleton.

// ExampleViewModel.tsx
import { ViewModel, provideByClass } from 'inversify-react-mvvm';
import { useParams } from 'react-router';
import { inject } from 'inversify';
import { SomeService } from './services/some-service.ts';

@provideByClass
export class ExampleViewModel extends ViewModel {

  // you can make full use of property injection
  @inject(SomeService) private someService!: SomeService;

  public content = "Wow, such empty";

  private params!: { id: string };

  public _bind = () => {
    // by overriding this method you can use hooks/effects/state
    this.params = useParams();
  }

  public onLoad = () => {
    console.log('~= componentDidMount');
    // place your favorite ajax calls here
  }

  public onLeave = () => {
    console.log('~= componentWillUnmount');
    // detach event handlers, dispose, etc.
  }
}

The (consumer) component

// Example.tsx
import { ExampleViewModel } from './ExampleViewModel';
import { useVm } from 'inversify-react-mvvm';

export const Example = (props: any) => {

  const vm = useVm(MenuViewModel);

  return (
    <div>{ vm.content }</div>
  );
};

The configuration

// App.tsx (or alike)
import { buildProviderModule } from 'inversify-binding-decorators';
import { Container } from 'inversify';
import { BrowserRouter as Router } from 'react-router-dom';
import { InversifyProvider } from 'inversify-react-mvvm';

const container = new Container();
// registers all bindings from inversify-inject-decorators
container.load(buildProviderModule())

export const App = () => {
  return  (
    <InversifyProvider container={container}>
      <Router>
        <Example />
      </Router>
    </InversifyProvider>
  )
}