All Projects → yojona → mafuba

yojona / mafuba

Licence: other
Simple state container for react apps.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to mafuba

RxReduxK
Micro-framework for Redux implemented in Kotlin
Stars: ✭ 65 (+225%)
Mutual labels:  flux, state, store
Flooks
🍸 A state manager for React Hooks
Stars: ✭ 201 (+905%)
Mutual labels:  flux, state, store
Reatom
State manager with a focus of all needs
Stars: ✭ 567 (+2735%)
Mutual labels:  flux, state, store
vuex-but-for-react
A state management library for React, heavily inspired by vuex
Stars: ✭ 96 (+380%)
Mutual labels:  flux, state
temperjs
State management for React, made simple.
Stars: ✭ 15 (-25%)
Mutual labels:  flux, state
mutable
State containers with dirty checking and more
Stars: ✭ 32 (+60%)
Mutual labels:  flux, state
Svelte Store Router
Store-based router for Svelte
Stars: ✭ 54 (+170%)
Mutual labels:  state, store
Almin
Client-side DDD/CQRS for JavaScript.
Stars: ✭ 477 (+2285%)
Mutual labels:  flux, state
Little State Machine
📠 React custom hook for persist state management
Stars: ✭ 654 (+3170%)
Mutual labels:  flux, state
Tiny Atom
Pragmatic and concise state management.
Stars: ✭ 109 (+445%)
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 (+435%)
Mutual labels:  flux, state
Redux Zero
A lightweight state container based on Redux
Stars: ✭ 1,977 (+9785%)
Mutual labels:  flux, state
Store
A beautifully-simple framework-agnostic modern state management library.
Stars: ✭ 204 (+920%)
Mutual labels:  state, store
Vue State Store
📮 VSS (Vue State Store) - Vue State Management (with Publish & Subscribe pattern)
Stars: ✭ 128 (+540%)
Mutual labels:  state, store
relite
a redux-like library for managing state with simpler api
Stars: ✭ 60 (+200%)
Mutual labels:  flux, store
Iostore
极简的全局数据管理方案,基于 React Hooks API
Stars: ✭ 99 (+395%)
Mutual labels:  state, store
knockout-store
State management for Knockout apps.
Stars: ✭ 37 (+85%)
Mutual labels:  state, store
Westore
更好的小程序项目架构
Stars: ✭ 3,897 (+19385%)
Mutual labels:  state, store
Freezer
A tree data structure that emits events on updates, even if the modification is triggered by one of the leaves, making it easier to think in a reactive way.
Stars: ✭ 1,268 (+6240%)
Mutual labels:  flux, store
Retalk
🐤 The Simplest Redux
Stars: ✭ 168 (+740%)
Mutual labels:  flux, state


Simple state container for React and React Native apps.

Installation

npm i mafuba

Create your Store

import Mafuba from 'mafuba'

export default new Mafuba({
  name: 'Piccolo Daimaō',
  age: 292,
  items: ['Makosen']
})

Link the Store to your App

import React, { Component } from 'react'
import Store from './Store'

export default class App extends Component {
  componentDidMount () {
    Store.link(this)
  }
  render () {
    return <Box />
  }
}

Now all components in your app will respond to state changes.

import React, { Component } from 'react'
import Store from '../Store'

export default class Box extends Component {
  render () {
    return (
      <div>
        <span> {Store.data.name} </span>
        <span> {Store.data.age} </span>
        <span> {Store.data.items.length} </span>

        <button onClick={() => Store.setState({age: Store.data.age + 1})}>Add one</button>
      </div>
    )
  }

Alternatively you can link only the components that use the Store

import React, { Component } from 'react'
import Store from '../Store'

export default class Box extends Component {
  componentDidMount () {
    Store.link(this)
  }
  render () {
    return (
      <div>
        <span> {Store.data.name} </span>
        <span> {Store.data.age} </span>
        <span> {Store.data.items.length} </span>

        <button onClick={() => Store.setState({age: Store.data.age + 1})}>Add one</button>
      </div>
    )
  }

Methods

You can set methods to manipulate the State.

import Mafuba from 'mafuba'

export default new Mafuba({
  data: {
    counter: 0
  },
  methods: {
    addOne () {
      this.setState({counter: this.data.counter + 1})
    }

  }
})

Another example

Methods.js

export function addOne () {
  this.setState({counter: this.data.counter + 1})
}

export function substractOne () {
  this.setState({counter: this.data.counter - 1})
}

Store.js

import Mafuba from 'mafuba'
import * as Methods from './Methods'

export default new Mafuba({
  data: {
    counter: 0
  },
  methods: {
    addOne: Methods.addOne,
    substractOne: Methods.substractOne,
  }
})

Dispatch

You can use the dispatch() function to dispatch actions instead of setting methods.

Store.js

import Mafuba from 'mafuba'

export default new Mafuba({
  counter: 0
})

App.js

import Store from './Mafuba/Store'
import * as Actions from './Mafuba/Methods'

class App extends Component {
  componentDidMount () {
    Store.link(this)
  }
  render () {
    return (
      <div className='App'>
        <p>{Store.data.counter}</p>

        <button onClick={() => { Store.dispatch(Actions.addOne) }}> Add one </button>
        <button onClick={() => { Store.dispatch(Actions.substractOne) }}> Substract one </button>

      </div>
    )
  }
}

Note. that if you prefer to use the dispatch function instead of establishing methods, you must import the actions when you need them.

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