0.0.5 • Published 3 months ago

vanrx v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Vanrx

vanrx is an ultra-lightweight Redux addon for VanJS, designed to enhance your VanJS applications with a simple yet powerful state management solution.

Features

  • Zero dependencies: Works out of the box with VanJS.
  • Simplicity: Easy to integrate and use within your VanJS projects.
  • Lightweight: Adds minimal overhead to your application.

Installation

To use vanrx in your project, simply include it alongside VanJS.

npm install vanrx

Usage

vanrx is designed to be simple and easy to use. To get started, simply create a state, give a name, an initial state, and then you can create as many reducers as you want.

import van from "vanjs-core";
import vanrx from "vanrx";

const [createReducer, dispatch, createSelector] = vanrx("Counter", {
  count: 0,
});

createReducer("increment", (state, increment) => {
  return {
    ...state,
    count: state.count + increment,
  };
});

const count = createSelector((state) => state.count);

function Counter() {
  return van.tags.div(
    () => count.val,
    van.tags.button({ onclick: () => dispatch("increment", 1) }, "Increment"),
  );
}

van.add(document.body, Counter())

Effects

As you might already know, vanrx is a Redux-like state management library, and it also supports effects for async states. You can create effects by using the createEffect function.

import van from "vanjs-core";
import vanrx from "vanrx";

const [createReducer, dispatch, createSelector, createEffect] = vanrx("Counter", {
  count: 0,
});

createReducer("increment", (state, increment) => {
  return {
    ...state,
    count: state.count + increment,
  };
});

createReducer("decrement", (state, increment) => {
  return {
    ...state,
    count: state.count - increment,
  };
});

createEffect("increment", () => {
  let timerId;
  return {
    effect: (dispatch) => {
      timerId = setTimeout(() => {
        dispatch("decrement", 2);
      }, 2000);
    },
    dispose: () => {
      clearTimeout(timerId);
    },
  };
});

const count = createSelector((state) => state.count);

function Counter() {
  return van.tags.div(
    () => count.val,
    van.tags.button({ onclick: () => dispatch("increment", 1) }, "Increment"),
  );
}

van.add(document.body, Counter())

License

vanrx is open source software. It is free to use and modify under the terms of the MIT license.

0.0.5

3 months ago

0.0.4

3 months ago

0.0.3

3 months ago

0.0.2

3 months ago

0.0.1

3 months ago