# mafuba

> Simple state container for React apps

Latest version **1.0.8** (published 2018-11-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install mafuba
pnpm add mafuba
yarn add mafuba
bun add mafuba
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2018-11-26 |
| First published | 2018-07-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jonathan Ayala |
| Maintainers | yojona |
| Keywords | mafuba, state, react, reactive, flux, redux, javascript, react native |

## Links

- npm: https://www.npmjs.com/package/mafuba
- Repository: https://github.com/yojona/mafuba
- Homepage: https://github.com/yojona/mafuba#readme
- Issues: https://github.com/yojona/mafuba/issues
- npm.io page: https://npm.io/package/mafuba

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 1.0.8 (latest) — 2018-11-26
- 1.0.7 — 2018-09-18
- 1.0.6 — 2018-07-13
- 1.0.5 — 2018-07-13
- 1.0.4 — 2018-07-13
- 1.0.3 — 2018-07-12
- 1.0.2 — 2018-07-12
- 1.0.1 — 2018-07-11
- 1.0.0 — 2018-07-11

## README

<p align="center">
<img src="https://i.imgur.com/FdNnX0r.png"><br />
Simple state container for React and React Native apps.
</p>

### Installation
```
npm i mafuba
```

### Create your Store
``` javascript
import Mafuba from 'mafuba'

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

### Link the Store to your App
``` javascript
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.

``` javascript
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
``` javascript
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.
```javascript
import Mafuba from 'mafuba'

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

  }
})

```
#### Another example
Methods.js
``` javascript
export function addOne () {
  this.setState({counter: this.data.counter + 1})
}

export function substractOne () {
  this.setState({counter: this.data.counter - 1})
}
```
Store.js
``` javascript
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
```javascript
import Mafuba from 'mafuba'

export default new Mafuba({
  counter: 0
})
```
App.js
```javascript
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.

---
_Source: https://npm.io/package/mafuba · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
