GithubHelp home page GithubHelp logo

rxjs-class-react-hook's Introduction

rxjs-class-react-hook

This module is a simple class to extend that will help you create rxjs stores.

How to use it

Store

import { Store } from "rxjs-class-react-hook";

export interface AppState {
    online: boolean;
    lang: string;
    theme: string;
}

const initialState: AppState = {
    online: true,
    lang: "en",
    theme: "blue",
};

class AppStore extends Store<AppState> {
    setOnline(online: boolean) {
        // Setter that merges the argument given into current state
        this.merge({ online });
    }

    setTheme(theme: string) {
        // Setter that merges the argument given with initialState and stores them in current state
        this.replace({
            online: true,
            lang: "en",
            theme,
        });
    }

    initialize() {
        // Resets the store to its initial state
        this.reset();
    }
}

const appStore = new AppStore(initialState);

export default appStore;

PersistStore

Much like Store, PersistStore is a different implement implementation which enables to persist the data. By default it auto loads and auto saves on init/changes and can be disabled.

A LocalStoragePersistor is packaged to enable saving to localStorage, but you can write your own persistors.

Selector

Selector is a small class helper to keep selectors. It is to extend for helping accessing data.

import { Selector } from "rxjs-class-react-hook";

export interface UserState {
    accessToken?: string;
    loginFailed: boolean;
}

export default class UserSelector extends Selector<UserState> {
    isOnline() {
        return Boolean(this.state.accessToken);
    }

    hasLoginError() {
        return this.state.loginFailed;
    }
}

The React hook

By setting the store as shown above, appStore will have a method available appStore.useState(). This method returns the current state of the store and refreshes as React.useState() when a change is applied.

To update the store, you just have to call the methods we defined earlier (e.g. appStore.initialize() will reset the store state to initialState)

rxjs-class-react-hook's People

Contributors

charlesgael avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.