1.0.2 • Published 2 years ago

react-reservice v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-reservice

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

A minimal react state management via proxy

Install

npm install react-reservice

Usage

import React from 'react';
import Service, { useService, serviceSelector } from 'react-reservice';

// Create service class
class YourService extends Service {
  constructor() {
    super();
    this.context = {
      // Set default context value
      username: 'Mark',
      balance: 1000
    };
  }

  updateUsername = (username: string) => {
    // Update context value directly
    this.context.username = username;
  }
}

// Create service instance
const yourService = new YourService();

// Create custom hooks by passing service instance and value selector
const useYourService = (subscriptionKeys: string[]) => useService(yourService, serviceSelector(subscriptionKeys));

// Bind the hooks with your component
const Username: FC = () => {
  // The Component will render only when `username` changed
  const { username } = useYourService(['username']);

  return <div>{username}</div>;
}

// Call service function directly in anywhere!!
yourService.updateUsername('John')
1.0.2

2 years ago

1.0.1

2 years ago