1.0.20 • Published 12 months ago

crown-state v1.0.20

Weekly downloads
-
License
ISC
Repository
github
Last release
12 months ago

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

1.0.20

12 months ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago