npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@relax-js/relax

v0.0.21

Published

Extremely Simple and Flexible JavaScript State Management Library

Downloads

24

Readme

Relax - A Promise Based State Management Library

npm version

A spin-off of Redux. Rewritten from scratch to simplify how you manage state. No more action types, switch statements, middleware, and especially no reducers! Plus it's tiny: 650 B gzipped! That's ~70% smaller than Redux: 2.23kb gzipped.

Basic Usage:

Relax Tutorial

Complimentary Libraries


Create Store

This is the most basic setup of creating a store. To read about the parameters it accepts, such as setting an initial state, see here

const store = createStore();

Now say you want to update the value of a DOM element every time the state is changed.

const rerender = ({ state }) => {
    // code to update a DOM value
    element.value = state.value;
}

store.subscribe(rerender);

Create an action to change the value in state

const updateValue = (newValue) => {
    return {
        value: newValue,
    }
}

// Dispatch the action
store.dispatch(updateValue(47))
console.log(store.getState()) // { value: 47 }

That was a super basic example. If you're familiar with Redux you may be wondering where the reducer is --- there are no reducers! What you return from your action is merged with the current state.

If you're thinking, "I can see this getting messy really fast" don't worry, managing different branches of state can be pretty simple! See here to continue reading on that.

Asyncronous Actions

Here's a basic example on asyncronous actions. To read more on this, see here.

// asynchronous action
const fetchData = (api) => {
    return fetch(api)
        .then(data => data)
        .then(result => ({
            apiData: result,
        });
}

store.dispatch(fetchData('/someApi'));
// When fetch promise resolves
console.log(store.getState()) // { value: 47, apiData: {...} }

Thenable Actions

Async actions are very common and sometimes callbacks to those actions are desireable.

Well, now you chain .then to dispatch (same with any Promise methods: .catch, .finally, etc.)

store.dispatch(fetchData('/someApi')).then(() => {
    store.dispatch(runAfterFetch());
});

// Btw, you can chain `.then` to synchronous actions too :)
store.dispatch(updateValue(100)).then(() => {
    // do whatever after value is updated
});

But wait, there's more to dispatch and .then. It provides an object of parameters!

store.dispatch(...).then(({ result, state, dispatch }) => {})

Dispatch Response

Property | Description ---|--- result | the return value from your action (what changed) state | current state dispatch | same as store.dispatch

Subscribe Response

Property | Description ---|--- ... | same response as dispatch unsubscribe | function to unsubscribe: unsubscribe()

Debugging

You can hook up to the Redux Dev Tools extension, if you have it installed, like this:

const devTools = window.__REDUX_DEVTOOLS_EXTENSION__.connect({
    name: `Instance Name`, // optional
});
devTools.init(store.getState());
store.subscribe(({ result, state }) => {
    const action = (result && result._action) || 'state changed';
    devTools.send(action, state);
});

Warning • I don't recommend using this in production so be sure to wrap it in process.env.NODE_ENV !== 'production' to avoid potential issues. • This is a basic example to show state diffs. Other dev tools features would require more logic.

Typically with action creators in Redux you'd create a type which would also display in the dev tools.

This is how you pass a type with Relax. Take the action updateValue for example:

const updateValue = (newValue) => ({
    _action: 'updateValue',
    value: newValue
});

_action is a string and is helpful with debugging in dev tools. You could subscribe and watch for an _action if you really wanted to.