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

xmodel-react

v1.0.2

Published

OOP style state management for React

Downloads

4

Readme

Xmodel for React

Published Date: 16 Aug 2022 (v1.0.2)

Only for React apps.

Visit Github page for other frameworks (Solid.js, Svelte).

    npm i xmodel-react
    # or 
    yarn add xmodel-react

Advantages

  • Model states can be both global or local.
  • Can share model codes across applications/framework with minimum code refactor
  • Intuitive for MVVM or MVI architecture
  • Less boilerplate/ Straightforward

Intro

Tired of Redux or using the useReducer or other Flux pattern? This library eliminitates your decision time on state management instead focus on building the actual buisness logic in OOP style.

A good design pattern is that your buisness logic should be never be tied with state management functions i.e. The core buisness logic must be independent from your app architecture thus it has to be loosely coupled with other components in your architecture.

Models

Models are the base component of the buisness logic where each model represent the specific domain-logic. MVVM, MVC, MVI and many uses the model in their architecture, hence the model code is portable for any framework. Thus you can port your model created for this library anywhere with minimum or no changes.

API


Model

Base abstract class that need to be extended by your model class.

PrimitiveModel

PrimitiveModel is used when the model has only a single primitive variable and has its own set of functions to modify or reuse.

constructor(value: Primitive)
value: Primitive
valueOf(): Primitive

type Primitive = string | number | boolean | null | undefined | BigInt | Symbol

ArrayModel

Array model is used for managing list of models where each model item can be modified directly. Native javascript array of model will not be reactive, thus you need to implement by extending this class for reactive collection of models.

constructor(Model: typeof Model) // Here you pass the Model class not the instance
list: Model[]
at(i: number): Model
push(item: Model): void
pop(i?: number, n?: number): Model | undefined 

useModel(modelClass: Model, args?: string[])

This function is used to convert your model class into reactive state which can be used anywhere in your react application.

Note: It can be called outside the react component thus making it available globally.


Example (Todo App)

// ./models/Todo.ts
import { Model } from "xmodel-react";

class Todo extends Model {
    task: string;
    isDone: boolean;

    constructor(task: string) {
        super();
        this.task = task;
        this.isDone = false;
    }

    changeTask(task: string) {
        this.task = task;
    }

    toggleDone() {
        this.isDone = !this.isDone;
    }
}

export default Todo;
// ./models/TodoList.ts
import { ArrayModel } from "xmodel-react";
import Todo from "./Todo";

class TodoList extends ArrayModel<Todo> {
  constructor() {
    super(Todo);
  }

  add(task: string) {
    this.push(new Todo(task));
    // push method is available on ArrayModel
    // If you need use native methods of array you can directly modify on this.list
  }

  delete(i: number) {
    this.pop(i);
    // This pop function can remove item at any index like in Python
    // Not only removes 1 element but also subsequent n items by passing it as second arg.
  }
}

export default TodoList;
// ./App.tsx
import "./App.css";

import TodoAdd from "./components/TodoAdd";
import TodoTask from "./components/TodoTask";

import TodoList from "./models/TodoList";

import { useModel } from "xmodel-react";

const todos = useModel(TodoList);

function App() {
  return (
    <div className="App">
      <TodoAdd add={todos.add} />
      {todos.list.map((todo, i) => (
        <TodoTask
          task={todo.task}
          isDone={todo.isDone}
          onToggle={() => todos.$.toggleDone(i)}
          onDelete={() => todos.delete(i)}
        />
      ))}
    </div>
  );
}

export default App;