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

react-peppermint

v1.1.3

Published

Fresh state management for React 🌿

Downloads

90

Readme

react-peppermint

Fresh state management for React 🌿

ci npm version license

The gist

import { action, useViewModel, viewModel } from 'react-peppermint';

//
// View model
//

@viewModel
class CounterVm {

    public count = 0;

    @action
    public increment() {
        this.count++;
    }
}

//
// Component
//

function Counter() {
    const vm = useViewModel(CounterVm)
    return (
      <>
        <h1>{vm.count}</h1>
        <button onClick={vm.increment}>
          Increment
        </button>
      </>
    );
}

Installation

yarn add react-peppermint

or

npm install --save react-peppermint

Provider

React Peppermint includes a <Provider /> component, which makes the view models available to the rest of your application:

import * as React from 'react';
import { createRoot } from 'react-dom/client';
import { Provider } from 'react-peppermint';
import App from './app';
import { resolver } from './resolver';

const root = createRoot(document.getElementById("root"));

root.render(
  <Provider resolver={resolver}>
    <App />
  </Provider>
);

The provider needs a Resolver. The resolver is a dependency injection container. React Peppermint is not tied to any specific container, use poor man's DI or your container of choice.

To keep it simple in this example we use a standard JavaScript Map as our DI container:

import { IResolver } from 'react-peppermint';
import { AppVm } from './appVm';
import { CounterVm } from './counterVm';

const container = new Map();
container.set(AppVm, new AppVm());
container.set(CounterVm, new CounterVm());

// Implement the IResolver interface
export const resolver: IResolver = {
    get: (key: any) => {
        return container.get(key);
    }
};

[!TIP] We recommend the excellent peppermint-di container :wink:

View models

View models hold state and logic to be used by your components.

First, annotate the class with the viewModel decorator. Then, add fields and method as usual.

Now, in order for a view-model method to cause a refresh of the component it's connected to, we need to annotate it with a decorator. There are four types of method decorators in React Peppermint:

  1. action - This is the basic method decorator. It causes any connected component to re-render when the method is done.
  2. broadcast - This decorator will cause all connected components to re-render, whether they are connected to this view model or any other view model.
  3. activate - Instructs React Peppermint to invoke this method after the first time the component is mounted (similarly to componentDidMount).
  4. deactivate - Instructs React Peppermint to invoke this method right before the component will unmount (similarly to componentWillUnmount).
import { action, activate, deactivate, viewModel } from 'react-peppermint';

@viewModel
class CounterVm {

    public count = 0;

    @activate
    public activate() {
        console.log('Hello!');
    }

    @action
    public increment() {
        this.count++;
    }

    @deactivate
    public activate() {
        console.log('Bye!');
    }
}

Class components

To connect a view model to a class component, use the withViewModel high order component (don't worry if you are not familiar with the HOC concept, you can think of it as a simple function).

import * as React from 'react';
import { withViewModel } from 'react-peppermint';
import { CounterVm } from './counterVm';

class Counter extends React.Component<CounterVm> {
    public render() {
        return (
            <>
                <h1>{this.props.count}</h1>
                <button onClick={this.props.increment}>
                    Increment
                </button>
            </>
        );
    }
}

// Using the 'withViewModel' HOC here:
export default withViewModel(CounterVm)(Counter);

Function components

You can connect function components to view models by using the withViewModel HOC, exactly like it's done with class components. However, higher-order components are not commonly used in modern React code. Instead, hooks are the common way to go.

The recommended way to connect a function component to a view model is by using the useViewModel hook:

import * as React from 'react';
import { useViewModel } from 'react-peppermint';
import { CounterVm } from './counterVm';

export default function Counter() {
    const vm = useViewModel(CounterVm)
    return (
      <>
        <h1>{vm.count}</h1>
        <button onClick={vm.increment}>
          Increment
        </button>
      </>
    );
}

Changelog

The changelog can be found here.