Git Product home page Git Product logo

crown-state's Introduction

Crown State

Crown State is a lightweight state management library. Just listens to state and sends changes to page or component that attach

Installation

Crown State is available as a package on NPM and YARN:

# NPM
npm install crown-state
# YARN
yarn add crown-state

Documantation

The Crown State docs are located at crown-state

Create a global state object

Create a new global state and export it, you can use with type and initial values

// user-state.ts

import State from "crown-state";

//Initial State Type
type UserType = {
  age: number;
  name: string;
};

export const $user = new State<UserType>({
  age: 27,
  name: "Selçuk",
});

Listen the state

import { $user } from "../user-state";
import { Attachment } from "crown-state";

/**
 * This method attaches to the relevant state and returns an Attachment 
 * type value. In case of any change on this state, both current state 
 * and previous state values can be accessed via the callback method.
 */
const att = Attachment = $user.attach((value, previousValue) => {
  console.log(value);
  console.log(previousValue);
});

/**
 * If you don't want to listen to the changes on this state, 
 * you can use the detach method.
 */
att.detach();

Update the state

import { $user } from "../user-state";

// To update the state just pass the values to the update method
$user.update({
  name: 'changed name',
  age: 28
});

// If you want to change only one field in state
$user.update({
  ...$user.value, // value gives you the current state value
  name: 'changed name'
});

License

ISC

crown-state's People

Contributors

aselcuk avatar

Stargazers

Mustafa Savul 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.