Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @relax-js/relax with all npm packages installed. Try it out:

var relax = require("@relax-js/relax")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@relax-js/relax v0.0.21

Extremely Simple and Flexible JavaScript State Management Library

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;


Create an action to change the value in state

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

// Dispatch the action
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,

// 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(() => {

// 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

resultthe return value from your action (what changed)
statecurrent state
dispatchsame as store.dispatch

Subscribe Response

...same response as dispatch
unsubscribefunction to unsubscribe: unsubscribe()


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


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free