├── .babelrc
├── .editorconfig
├── .gitignore
├── .prettierrc
├── .travis.yml
├── CHANGELOG.md
├── LICENSE
├── README.md
├── inspect.js
├── package.json
├── rollup.config.js
├── src
├── __tests__
│ ├── __snapshots__
│ │ ├── provider.test.js.snap
│ │ └── state.test.js.snap
│ ├── index.test.js
│ ├── provider.test.js
│ └── state.test.js
├── index.js
├── provider.js
└── state.js
└── yarn.lock
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": ["env", "react"]
3 | }
4 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 | [*]
3 | # Change these settings to your own preference
4 | indent_style = space
5 | indent_size = 2
6 | # We recommend you to keep these unchanged
7 | end_of_line = lf
8 | charset = utf-8
9 | trim_trailing_whitespace = true
10 | insert_final_newline = true
11 | [*.md]
12 | trim_trailing_whitespace = false
13 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | dist
2 | node_modules
3 | npm-debug.log
4 | .DS_Store
5 | coverage
6 |
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "semi": false,
3 | "singleQuote": true,
4 | "jsxBracketSameLine": true
5 | }
6 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - 8
4 | notifications:
5 | email: false
6 | script:
7 | - npm run test
8 | after_success:
9 | - npm install -g codecov
10 | - codecov
11 | branches:
12 | only:
13 | - master
14 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 |
3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4 |
5 |
6 | ## [2.0.4](https://github.com/vesparny/statty/compare/v2.0.2...v2.0.4) (2018-04-11)
7 |
8 |
9 | ### Bug Fixes
10 |
11 | * Improved memory usage for state management ([#18](https://github.com/vesparny/statty/issues/18)) ([50fd5d7](https://github.com/vesparny/statty/commit/50fd5d7))
12 | * not throwing errors when multiple updates happen ([ea9a92b](https://github.com/vesparny/statty/commit/ea9a92b))
13 |
14 |
15 |
16 |
17 | ## [2.0.3](https://github.com/vesparny/statty/compare/v2.0.2...v2.0.3) (2017-10-15)
18 |
19 |
20 | ### Bug Fixes
21 |
22 | * setState when needed in case children update state in cWM/cDM closes #15 (#16) ([5ebe2c2](https://github.com/vesparny/statty/commit/5ebe2c2)), closes [#15](https://github.com/vesparny/statty/issues/15) [#16](https://github.com/vesparny/statty/issues/16)
23 |
24 |
25 |
26 |
27 | ## [2.0.2](https://github.com/vesparny/statty/compare/v2.0.1...v2.0.2) (2017-10-03)
28 |
29 |
30 | ### Bug Fixes
31 |
32 | * remove useless typeof comparison ([87f3f6a](https://github.com/vesparny/statty/commit/87f3f6a))
33 |
34 |
35 |
36 |
37 | ## [2.0.1](https://github.com/vesparny/statty/compare/v2.0.0...v2.0.1) (2017-10-03)
38 |
39 |
40 | ### Bug Fixes
41 |
42 | * if selectors returns undefined or null always rerender ([dc7b8b6](https://github.com/vesparny/statty/commit/dc7b8b6))
43 |
44 |
45 |
46 |
47 | # [2.0.0](https://github.com/vesparny/statty/compare/v1.0.0...v2.0.0) (2017-10-03)
48 |
49 |
50 | ### Bug Fixes
51 |
52 | * throws if updaters invoke other updaters ([67e8a26](https://github.com/vesparny/statty/commit/67e8a26))
53 |
54 |
55 | ### Chores
56 |
57 | * do not use xtend anymore ([08085de](https://github.com/vesparny/statty/commit/08085de))
58 |
59 |
60 | ### Performance Improvements
61 |
62 | * improve performance with nested subscription by re-rendering only if necessary ([3e473fa](https://github.com/vesparny/statty/commit/3e473fa))
63 |
64 |
65 | ### BREAKING CHANGES
66 |
67 | * updaters MUST return a complete new state, without
68 | mutating the old one
69 |
70 |
71 |
72 |
73 | # 1.0.0 (2017-09-25)
74 |
75 |
76 | ### Features
77 |
78 | * add inspect ([97b6368](https://github.com/vesparny/statty/commit/97b6368))
79 | * initial version ([ad8e5ba](https://github.com/vesparny/statty/commit/ad8e5ba))
80 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017-present Alessandro Arnodo
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # statty
2 |
3 | > A tiny and unobtrusive state management library for React and Preact apps
4 |
5 | [](https://travis-ci.org/vesparny/statty)
6 | [](https://codecov.io/github/vesparny/statty)
7 | [](https://david-dm.org/vesparny/statty)
8 | [](https://www.npmjs.com/package/statty)
9 | [](https://npm-stat.com/charts.html?package=statty&from=2017-05-19)
10 | [](http://standardjs.com/)
11 | [](https://github.com/vesparny/statty/blob/master/LICENSE)
12 |
13 | The current size of `statty/dist/statty.umd.min.js` is:
14 |
15 | [](https://unpkg.com/statty/dist/)
16 |
17 | ## The problem
18 |
19 | Most of the time, I see colleagues starting React projects setting up Redux + a bunch of middlewares and store enhancers by default, regardless of the project nature.
20 |
21 | Despite Redux being awesome, [it's not always needed](https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367) and it may slow down the process of onboarding new developers, especially if they are new to the React ecosystem (I have often seen colleagues being stuck for hours trying to understand what was the proper way to submit a simple form).
22 |
23 | React already comes with a built-in state management mechanism, `setState()`. Local component state is just fine in most of the cases.
24 |
25 | In real world apps we often have app state, and sometimes it becomes annoying to pass it down the entire component tree, along with callbacks to update it, via props.
26 |
27 | ## This solution
28 |
29 | `statty` is meant to manage app-wide state and can be thought of as a simplified version of Redux.
30 |
31 | It [safely](https://medium.com/@mweststrate/how-to-safely-use-react-context-b7e343eff076) leverages context to expose application state to children, along with a function to update it when needed.
32 |
33 | The update function acts like Redux dispatch, but instead of an action, it takes an `updater` function as a parameter that returns the new state.
34 |
35 | This way it's easy to write testable updaters and to organize them as you prefer, without having to write boilerplate code.
36 |
37 | ## Table of Contents
38 |
39 | - [Installation](#installation)
40 | - [Usage](#usage)
41 | - [API](#api)
42 | - [Examples](#examples)
43 | - [Inspiration](#inspiration)
44 | - [LICENSE](#license)
45 |
46 | ## Installation
47 |
48 | This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Check them out if you don't have them locally installed.
49 |
50 | ```sh
51 | $ npm i statty
52 | ```
53 |
54 | Then with a module bundler like [rollup](http://rollupjs.org/) or [webpack](https://webpack.js.org/), use as you would anything else:
55 |
56 | ```javascript
57 | // using ES6 modules
58 | import statty from 'statty'
59 |
60 | // using CommonJS modules
61 | var statty = require('statty')
62 | ```
63 |
64 | The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com):
65 |
66 | ```html
67 |
68 | ```
69 |
70 | You can find the library on `window.statty`.
71 |
72 | ## Usage
73 |
74 | ```jsx
75 | // https://codesandbox.io/s/rzpxx0w34
76 | import React from 'react'
77 | import { render } from 'react-dom'
78 | import { Provider, State } from 'statty'
79 | import inspect from 'statty/inspect'
80 |
81 | // selector is a function that returns a slice of the state
82 | // if not specified it defaults to f => f
83 | const selector = state => ({ count: state.count })
84 |
85 | // updaters
86 |
87 | // updaters MUST be pure and return a complete new state,
88 | // like Redux reducers
89 | const onDecrement = state =>
90 | Object.assign({}, state, { count: state.count - 1 })
91 |
92 | const onIncrement = state =>
93 | Object.assign({}, state, { count: state.count + 1 })
94 |
95 | // Counter uses a component to access the state
96 | // and the update function used to execute state mutations
97 | const Counter = () => (
98 | (
101 |