1.0.20 • Published 9 months ago

starux v1.0.20

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

STARUX

Build Status Build Size Version Downloads

A small library for creating stores.

npm install starux
npm add starux

Create a vanilla store

import createStore from 'starux'

const userStore = createStore({
    initialState: {
        name: 'Jack',
        surname: 'Jackov'
    },
    reducers: {
        setName(state, name) {
            state.name = name
        },
        async setSurname(state, surname) {
            const isValid = await validateSurname(surname);
            if (isValid) {
                state.surname = surname;
            }
        },
        clear() {
            return {
                name: 'Jack',
                surname: 'Jackov'
            }
        }
    },
    selectors: {
        getName(state) {
            return state.name
        }
    }
})

Reducers turn into actions. You can pass multiple arguments to them. Selectors are used as an aid to avoid placing them in separate functions/objects etc.

Usage

userStore.actions.setName('Anna') // changes state.name to Anna
userStore.actions.setSurname('Baykova') // return Promise
await userStore.actions.setSurname('Baykova') // changes state.surname to Baykova

userStore.get() // returns state
userStore.get((state) => state.name) // returns state.name
userStore.get.getName() // returns state.name

const unsubscribe = userStore.subscribe((state) => doSomething(state)) // calls callback every state changing

Create a react store

import createHookStore from 'starux/react'

const userStore = createHookStore({
    initialState: {
        name: 'Jack',
        surname: 'Jackov'
    },
    reducers: {
        setName(state, name) {
            state.name = name
        },
        async setSurname(state, surname) {
            const isValid = await validateSurname(surname);
            if (isValid) {
                state.surname = surname;
            }
        },
        clear() {
            return {
                name: 'Jack',
                surname: 'Jackov'
            }
        }
    },
    selectors: {
        getName(state) {
            return state.name
        }
    }
})

Usage

const actions = userStore.useActions();
// ...
actions.setName(); // changes state.name to Anna
actions.setSurname('Baykova') // return Promise
const name = userStore.useSelector('getName') // returns state.name
const name = userStore.useSelector((state) => state.name) // returns state.name
const state = userStore.useSelector() // returns state
userStore.useSubscribe((state) => doSomething(state)) // calls callback every state changing
1.0.19

9 months ago

1.0.18

9 months ago

1.0.17

9 months ago

1.0.16

9 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.11

10 months ago

1.0.10

10 months ago

1.0.20

9 months ago

1.0.15

9 months ago

1.0.14

9 months ago

1.0.13

9 months ago

1.0.12

10 months ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

0.1.0

1 year ago