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

@jser-tang/react-mvc

v1.0.7

Published

A powerful tool for simplifying React state and behavior management.

Downloads

1

Readme

React-MVC

Description

A powerful tool for simplifying React state and behavior management.

Why choose react-mvc? Imagine you are developing an application that allows users to switch between multiple skin styles freely. Typically, you might use multiple sets of CSS styles to achieve this. However, what if the different skin styles not only differ in appearance but also in layout and interactions?

You might consider handling the different styles separately and abstracting common methods into utils for reuse. This is indeed a feasible solution, but the problem arises when you need to retain the data state while switching skins: the functions in utils are stateless.

At this point, you might consider using Redux to manage state and encapsulating common methods into dispatches, which are then called through mapStateToProps. While this can meet our requirements, it comes at the cost of writing a lot of boilerplate code, making the codebase abstract and hard to understand.

The react-mvc Solution This is precisely why we developed react-mvc. By separating logic from UI, react-mvc allows state and methods to be centrally managed, while the UI can still be freely implemented. No matter how complex the interface is, you can easily share state and methods within the scope. react-mvc provides a simple yet powerful way to manage state and common behaviors in complex applications. It not only reduces boilerplate code but also improves code readability and maintainability. With react-mvc, you can focus on building exceptional user interfaces without being bogged down by complex state management and common method calls. Try react-mvc now and experience unprecedented development convenience!

Key Features:

  • Scope: Unlike Redux's single global store, you can create multiple react-mvc instances and nest them. Different instances are distinguished by keys, and the granularity is up to you.
  • Separation of Logic and UI: Makes state and method management more streamlined and organized.
  • Simple and Efficient State Management: When state changes, it does not update the entire component tree, only the components that use the changed state.
  • Lifecycle Hooks: Provides lifecycle hook methods.
  • Ease of Use: No need for extensive boilerplate code; only a few APIs are required to get started easily.

Usage

npm i @jser-tang/react-mvc

API

  • AppController(key: string; ctrl: IExtendsCtrl)

Create a controller instance where you can manage your store, declaration cycle, and methods

  • withCtrl(conponent: React.FunctionComponent; )

Wrap components so that they have the ability to share data and call common methods

Example

controller.js

import { createController } from '@jser-tang/react-mvc';

const { Controller, withCtrl } = createController<IHomeProps>('appHome', {
    async init() {
        // The init method is called when the instance is created, where some initialization operations can be done, such as requesting and updating data
        const initBData = await fetch('xxxxx');
        // data.a.b -> ['a', 'b']
        this.setData(initBData, ['a', 'b']),
    },
    methods: {
        // You define your own public methods in the controller, inside which you can get the store data
        addCount() {
            const count = this.data.count;
            this.setData(count + 1, ['count']);
        },
    }
});

export { AppController: Controller, withCtrl };

app.jsx

import { ChildComponent } from './childComponent';
import { AppController } from './controller';

const App = () => {
    const initData = {
        count: 0,
        a: {
            b: void 0,
        }
    };
    return (
        <AppController store={props}>
            <ChildComponent />
        </AppController>
    );
};

childComponent.jsx

import { withCtrl } from './controller';

export const OnliveTipWrap = withCtrl((props) => {
    const { $ctrl: {data, addCount} } = props;
    return (
        <div>
            <p>
                {data.count}
            </p>
            <button onClick={addCount}> addCount </button>
        </div>
    );
});

License

MIT licensed.