All Projects → KidkArolis → Tiny Atom

KidkArolis / Tiny Atom

Licence: mit
Pragmatic and concise state management.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Tiny Atom

Redux Zero
A lightweight state container based on Redux
Stars: ✭ 1,977 (+1713.76%)
Mutual labels:  flux, functional, state, preact
stoxy
Stoxy is a state management API for all modern Web Technologies
Stars: ✭ 73 (-33.03%)
Mutual labels:  preact, state
RxReduxK
Micro-framework for Redux implemented in Kotlin
Stars: ✭ 65 (-40.37%)
Mutual labels:  flux, state
Dutier
The immutable, async and hybrid state management solution for Javascript applications.
Stars: ✭ 401 (+267.89%)
Mutual labels:  flux, minimal
hermes-js
Universal action dispatcher for JavaScript apps
Stars: ✭ 15 (-86.24%)
Mutual labels:  flux, functional
teaful
🍵 Tiny, easy and powerful React state management
Stars: ✭ 638 (+485.32%)
Mutual labels:  preact, state
Unistore
🌶 350b / 650b state container with component actions for Preact & React
Stars: ✭ 2,850 (+2514.68%)
Mutual labels:  state, preact
snap-state
State management in a snap 👌
Stars: ✭ 23 (-78.9%)
Mutual labels:  preact, state
Statty
A tiny and unobtrusive state management library for React and Preact apps
Stars: ✭ 516 (+373.39%)
Mutual labels:  state, preact
Reatom
State manager with a focus of all needs
Stars: ✭ 567 (+420.18%)
Mutual labels:  flux, state
Clean State
🐻 A pure and compact state manager, using React-hooks native implementation, automatically connect the module organization architecture. 🍋
Stars: ✭ 107 (-1.83%)
Mutual labels:  flux, state
mutable
State containers with dirty checking and more
Stars: ✭ 32 (-70.64%)
Mutual labels:  flux, state
temperjs
State management for React, made simple.
Stars: ✭ 15 (-86.24%)
Mutual labels:  flux, state
vuex-but-for-react
A state management library for React, heavily inspired by vuex
Stars: ✭ 96 (-11.93%)
Mutual labels:  flux, state
mafuba
Simple state container for react apps.
Stars: ✭ 20 (-81.65%)
Mutual labels:  flux, state
react-wisteria
Managing the State with the Golden Path
Stars: ✭ 18 (-83.49%)
Mutual labels:  functional, state
Alfa
Effortless React State Management.
Stars: ✭ 86 (-21.1%)
Mutual labels:  functional, state
Flooks
🍸 A state manager for React Hooks
Stars: ✭ 201 (+84.4%)
Mutual labels:  flux, state
hyperapp-example
hyperapp example
Stars: ✭ 14 (-87.16%)
Mutual labels:  preact, state
Almin
Client-side DDD/CQRS for JavaScript.
Stars: ✭ 477 (+337.61%)
Mutual labels:  flux, state


tiny atom logo

Pragmatic and concise state management.

npm size Build Status code style: standard

  • single store modified via actions
  • tiny api - easy to understand, easy to adapt
  • tiny size - 1KB, or 2KB with (p)react bindings
  • react and preact bindings included
  • react hooks support
  • highly optimised with batched rerenders
  • beautiful console logger
  • redux devtools integration

How is this different from redux? The key differences are:

  • Actions in tiny-atom are self contained units of business logic. They can read and update the state and dispatch other actions any number of times. This removes layers of boilerplate while preserving the benefits of redux like stores.
  • Tiny-atom includes useful utilities to make it completely sufficient for building application of any size.

Installation

$ npm install tiny-atom

Docs

Read the full docs or pick one of the highlights:

Example

import createAtom from 'tiny-atom'

const atom = createAtom({ unicorns: 0, rainbows: [] }, {
  incrementUnicorns ({ get, set }, n) {
    set({ unicorns: get().unicorns + n })
  },

  async fetchRainbows ({ set, actions }) {
    set({ loading: true })
    const { data: rainbows } = await axios.get('/api/rainbows')
    set({ rainbows, loading: false })
    actions.incrementUnicorns(1)
  }
})

atom.observe(atom => {
  console.log('atom', atom)
  const { rainbows, unicorns } = atom.get()
  render(unicorns).onClick(e => atom.actions.incrementUnicorns(10))
})

React Example

Provide the store:

import React from 'react'
import ReactDOM from 'react-dom'
import createAtom from 'tiny-atom'
import { Provider } from 'tiny-atom/react'

const atom = createAtom({ user: { name: 'Atom' } }, {
  message ({ get, set, swap, actions }, msg) {
    console.log(msg)
  }
})

ReactDOM.render((
  <Provider atom={atom}>
    <App />
  </Provider>
), document.querySelector('root'))

Connect using React hooks:

import React from 'react'
import { useAtom, useActions } from 'tiny-atom/react/hooks'

export default function Hello () {
  const user = useAtom(state => state.user)
  const { message } = useActions()

  return (
    <button onClick={() => message('hi')}>{user.name}</button>
  )
}

API

createAtom(initialState, actions, options)

Create an atom.

initialState

type: any default: {}

The initial state of the atom.

actions

type: object default: {}

An object with action functions. The signature of an action function is ({ get, set, swap, actions, dispatch }, payload). If you provide nested action objects or other structure, make sure to also specify an appropriate options.evolve implementation to handle your actions appropriately.

  • get() - returns the current state
  • set(patch) - updates the state with the patch object by merging the patch using Object.assign
  • swap(state) - replace the entire state with the provided one
  • dispatch - same as atom.dispatch, dispatches an action
  • actions - actions prebound to dispatch, i.e. actions.increment(1) is equivalent to dispatch('increment', 1)

options.evolve

type: function

A function that receives all of the dispatched action objects and calls the action functions. The function signature is (atom, action, actions). Note that atom in this place has an extra added function set, a function that is used to update the state, this function does not exist on the actual atom. The default implementation uses action.type to find the matching function in the actions object.

options.debug

type: function | function[] default: null

A function that will be called on each action and state update. The function is passed an info object of shape { type, atom, action, sourceActions, prevState }. Tiny atom comes with 2 built in debug functions tiny-atom/log and tiny-atom/devtools.

createAtom({ count: 1 }, {
  increment: ({ get, set }, payload) => set({ count: get().count + payload }),
  inc: ({ actions }, payload) => actions.increment(payload)
})

atom.get()

Get current state.

atom.get()
atom.get().feed.items

atom.set(update)

Update state by shallowly merging an update.

atom.set({ user })
atom.set({ entities: { ...get().entities, posts } })

atom.swap(state)

Replace the entire state with the provided one.

atom.swap(nextState)

atom.dispatch(type, payload)

Send an action

atom.dispatch('fetchMovies')
atom.dispatch('increment', 5)

atom.actions

A map of prebound actions. For example, if your actions passed to atom are

const actions = {
  increment ({ get, set }) {
    const { count } = get()
    set({ count: count + 1 })
  }
}

They will be bound such that calling atom.actions.increment(1) dispatches action with `dispatch('increment', 1).

atom.observe(cb)

Register a callback for when atom changes. Returns the unobserve function.

atom.observe(render)
atom.observe(atom => render(atom.get(), atom.dispatch))

atom.fuse(state, actions)

Extend atom's state and the action object. Convenient for composing atom from slices of state and actions from several modules.

const state = {
  project: { name: 'tiny-atom' }
}

const actions = {
  star: ({ get, set }) => set({
    project: { starred: true }
  })
}

atom.fuse(state, actions)

React / Preact bindings

For documentation on the set of react and preact components <Provider />, <Consumer />, connect, useAtom, useActions and useDispatch see react or preact docs.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].