Permalink
Browse files

Initial commit

  • Loading branch information...
diegohaz committed Mar 31, 2018
0 parents commit 93b7b5b469be4521784b51380f49e6589c3e56b9
@@ -0,0 +1,20 @@
{
"presets": [
[
"env",
{
"modules": false,
"loose": true
}
],
"stage-1",
"react"
],
"env": {
"test": {
"plugins": [
"transform-es2015-modules-commonjs"
]
}
}
}
@@ -0,0 +1,20 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]

# Change these settings to your own preference
indent_style = space
indent_size = 2

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
@@ -0,0 +1,29 @@
{
"parser": "babel-eslint",
"extends": [
"airbnb",
"prettier",
"prettier/react"
],
"plugins": [
"prettier"
],
"env": {
"jest": true,
"browser": true
},
"rules": {
"import/no-extraneous-dependencies": 0,
"react/forbid-prop-types": 0,
"react/jsx-filename-extension": 0,
"react/require-default-props": 0,
"prettier/prettier": [
"error",
{
"singleQuote": true,
"semi": false,
"trailingComma": "all"
}
]
}
}
@@ -0,0 +1,5 @@
.DS_Store
node_modules
coverage
dist
*.log
@@ -0,0 +1,9 @@
language: node_js
node_js:
- v6
script:
- npm run lint && npm test -- --coverage
cache:
- yarn
after_success:
- bash <(curl -s https://codecov.io/bash)
21 LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Diego Haz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
230 README.md
@@ -0,0 +1,230 @@
<p align="center">
<img src="logo/logo.svg" alt="constate logo" width="300" />
</p>
<br /><br />

<p align="center">
<a href="https://github.com/diegohaz/nod"><img alt="Generated with nod" src="https://img.shields.io/badge/generator-nod-2196F3.svg?style=flat-square" /></a>
<a href="https://npmjs.org/package/constate"><img alt="NPM version" src="https://img.shields.io/npm/v/constate.svg?style=flat-square" /></a>
<a href="https://travis-ci.org/diegohaz/constate"><img alt="Build Status" src="https://img.shields.io/travis/diegohaz/constate/master.svg?style=flat-square" /></a>
<a href="https://codecov.io/gh/diegohaz/constate/branch/master"><img alt="Coverage Status" src="https://img.shields.io/codecov/c/github/diegohaz/constate/master.svg?style=flat-square" /></a>
</p>
<br /><br />

> context + state = constate
Yet another React state management library that lets you work with local state and scale up to global state with ease.

## Install

npm:
```sh
npm i -S constate
```

Yarn:
```sh
yarn add constate
```

## Usage

### Local state

You can start by using local state. Make the state global only when you need it.

1. Create your `State` component:
```jsx
// CounterState.js
import React from 'react'
import { State } from 'constate'
export const initialState = {
count: 0,
}
export const actions = {
increment: amount => state => ({ count: state.count + amount }),
}
export const selectors = {
getParity: () => state => (state.count % 2 === 0 ? 'even' : 'odd'),
}
const CounterState = props => (
<State
initialState={initialState}
actions={actions}
selectors={selectors}
{...props}
/>
)
export default CounterState
```

2. Wrap your component with `CounterState`:
```jsx
// CounterButton.js
import React from 'react'
import CounterState from './CounterState'
const CounterButton = () => (
<CounterState>
{({ count, increment, getParity }) => (
<button onClick={() => increment(1)}>{count} {getParity()}</button>
)}
</CounterState>
)
export default CounterButton
```

### Global state

Whenever you need to share state between components and/or feel the need to have a global state, just follow these steps:

1. Pass `context` property to your `State` components:
```jsx
// CounterButton.js
import React from 'react'
import CounterState from './CounterState'
const CounterButton = () => (
<CounterState context="foo">
{({ increment }) => <button onClick={() => increment(1)}>Increment</button>}
</CounterState>
)
export default CounterButton
```
```jsx
// CounterValue.js
import React from 'react'
import CounterState from './CounterState'
const CounterValue = () => (
<CounterState context="foo">
{({ count }) => <div>{count}</div>}
</CounterState>
)
export default CounterValue
```
```jsx
// CounterParity.js
import React from 'react'
import CounterState from './CounterState'
const CounterParity = () => (
<CounterState context="foo">
{({ getParity }) => <div>{getParity()}</div>}
</CounterState>
)
export default CounterParity
```

2. Wrap your root component with `Provider`:
```jsx
// index.js
import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'constate'
import CounterButton from './CounterButton'
import CounterValue from './CounterValue'
import CounterParity from './CounterParity'
const App = () => (
<Provider>
<CounterButton />
<CounterValue />
<CounterParity />
</Provider>
)
ReactDOM.render(<App />, document.getElementById('root'))
```

### Overriding `CounterState` properties

This is still React, so you can pass new properties to `CounterState`, making it really composable.

First, let's change our `CounterState` so as to receive new properties:

```jsx
const CounterState = props => (
<State
{...props}
initialState={{ ...initialState, ...props.initialState }}
actions={{ ...actions, ...props.actions }}
selectors={{ ...selectors, ...props.selectors }}
/>
)
```

Now we can pass new `initialState`, `actions` and `selectors` to `CounterState`:

```jsx
export const initialState = {
count: 10,
}
export const actions = {
decrement: amount => state => ({ count: state.count - amount }),
}
const CounterButton = () => (
<CounterState initialState={initialState} actions={actions}>
{({ decrement }) => <button onClick={() => decrement(1)}>Decrement</button>}
</CounterState>
)
```

Those new members will work even if you use `context`.

### Global initial state

You can also pass `initialState` to `Provider`:

```jsx
const initialState = {
foo: {
count: 10,
},
}
const App = () => (
<Provider initialState={initialState}>
...
</Provider>
)
```

That way, components using `context=foo` will have that initial state.

## Testing

`actions` and `selectors` are pure functions. Testing is pretty straightfoward:

```js
import { initialState, actions, selectors } from './CounterState'
test('initialState', () => {
expect(initialState).toEqual({ count: 0 })
})
test('actions', () => {
expect(actions.increment(1)({ count: 0 })).toEqual({ count: 1 })
expect(actions.increment(-1)({ count: 1 })).toEqual({ count: 0 })
})
test('selectors', () => {
expect(selectors.getParity()({ count: 0 })).toBe('even')
expect(selectors.getParity()({ count: 1 })).toBe('odd')
})
```

## License

MIT © [Diego Haz](https://github.com/diegohaz)
BIN +16.5 KB logo/logo-black.png
Binary file not shown.
@@ -0,0 +1 @@
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 560 560"><defs><style>.cls-1{fill:none;stroke:#000;stroke-miterlimit:10;stroke-width:10px;}</style></defs><title>Artboard 1</title><path d="M130.6,309.11c-18.18,0-22.95-11.43-22.95-23.67s4.95-23.66,22.77-23.66c14,0,20.7,7.47,20.7,19h-9.81c-.63-7.11-3.51-11.16-11.16-11.16-9.9,0-12.69,6.84-12.69,15.74,0,8.73,2.7,15.48,12.87,15.48,7.65,0,10.35-3.78,11-10.44h9.81C151.12,301.73,144.73,309.11,130.6,309.11Z"/><path d="M179.11,309.11c-18.09,0-22.77-11.43-22.77-23.67s4.77-23.66,22.86-23.66c17.91,0,22.86,11.52,22.86,23.66C202.06,297.77,197.11,309.11,179.11,309.11Zm.09-39.5c-10.71,0-13,7.2-13,15.83s2.34,15.84,13,15.84c10.8,0,13.14-6.93,13.14-15.84C192.25,276.9,190.09,269.61,179.2,269.61Z"/><path d="M242.38,308.48V282.83c0-8.18-1.44-13-11.25-13s-11.88,4.77-11.88,13v25.65h-9.63V262.41h5.22l2.16,6.48c1.53-3.15,6.39-7.11,16.83-7.11,15.12,0,18.09,8.82,18.09,21.87v24.83Z"/><path d="M280.36,309.11c-15.57,0-22.23-6.12-22.23-17.82h9.45c0,6.3,3.6,10.17,12.6,10.17,8.19,0,10.8-3.06,11-5.76.27-10-32.13-3.33-32.13-19.79,0-8.1,5.58-14.13,20.16-14.13,14.94,0,20.25,7,20.25,17.19h-9c-.36-6-3.06-9.72-11.52-9.72-7.74,0-10.35,2.88-10.35,5.85,0,8.9,32,2.79,32,20.06C300.61,301.91,294.94,309.11,280.36,309.11Z"/><path d="M317.71,270.15v25.1c0,4.32,1.8,6.3,11.79,5.85v7.74c-15.21.9-21.6-2.7-21.6-13.59v-25.1h-6.57v-5.76l6.57-2V250.89h9.54v11.52H329.5v7.74Z"/><path d="M369.64,308.48l-2-6.93c-2.16,3.6-8.55,7.56-18.09,7.56-9.9,0-17.82-4.41-17.82-14.13,0-8.46,6-12.33,19.17-13.58L365.41,280c-.27-6.66-2.7-10.53-12-10.53-8.55,0-10.89,3.42-10.89,9.17l-9.36.1c0-9.54,4-16.92,20.16-16.92,18.81,0,21.51,8.1,21.51,20.51v26.19Zm-4.23-21.24-13.05,1.44c-8,.9-10.71,2.61-10.71,6.21,0,4.23,3.6,6.66,10.35,6.66,8.46,0,13.41-4.86,13.41-12.87Z"/><path d="M394.21,270.15v25.1c0,4.32,1.8,6.3,11.79,5.85v7.74c-15.21.9-21.6-2.7-21.6-13.59v-25.1h-6.57v-5.76l6.57-2V250.89h9.54v11.52H406v7.74Z"/><path d="M431.65,309.11c-18.54,0-23.13-11.43-23.13-23.76s4.68-23.57,23-23.57c17,0,21.78,13,20.61,27.44H418.51c.54,7.92,4.5,12.51,12.78,12.51,7.65,0,10.08-3.69,11-7.38h10.08C451.54,302.18,446.14,309.11,431.65,309.11Zm-1-39.86c-9,0-11.61,5.58-12.06,12.33h23.67s.09,0,0-.1.09-.08,0-.08C442.36,275.1,439.39,269.25,430.66,269.25Z"/><circle class="cls-1" cx="280" cy="280" r="275"/></svg>
BIN +15.8 KB logo/logo-white.png
Binary file not shown.
@@ -0,0 +1 @@
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 560 560"><defs><style>.cls-1{fill:#fff;}.cls-2{fill:none;stroke:#fff;stroke-miterlimit:10;stroke-width:10px;}</style></defs><title>Artboard 1</title><path class="cls-1" d="M130.6,309.11c-18.18,0-22.95-11.43-22.95-23.67s4.95-23.66,22.77-23.66c14,0,20.7,7.47,20.7,19h-9.81c-.63-7.11-3.51-11.16-11.16-11.16-9.9,0-12.69,6.84-12.69,15.74,0,8.73,2.7,15.48,12.87,15.48,7.65,0,10.35-3.78,11-10.44h9.81C151.12,301.73,144.73,309.11,130.6,309.11Z"/><path class="cls-1" d="M179.11,309.11c-18.09,0-22.77-11.43-22.77-23.67s4.77-23.66,22.86-23.66c17.91,0,22.86,11.52,22.86,23.66C202.06,297.77,197.11,309.11,179.11,309.11Zm.09-39.5c-10.71,0-13,7.2-13,15.83s2.34,15.84,13,15.84c10.8,0,13.14-6.93,13.14-15.84C192.25,276.9,190.09,269.61,179.2,269.61Z"/><path class="cls-1" d="M242.38,308.48V282.83c0-8.18-1.44-13-11.25-13s-11.88,4.77-11.88,13v25.65h-9.63V262.41h5.22l2.16,6.48c1.53-3.15,6.39-7.11,16.83-7.11,15.12,0,18.09,8.82,18.09,21.87v24.83Z"/><path class="cls-1" d="M280.36,309.11c-15.57,0-22.23-6.12-22.23-17.82h9.45c0,6.3,3.6,10.17,12.6,10.17,8.19,0,10.8-3.06,11-5.76.27-10-32.13-3.33-32.13-19.79,0-8.1,5.58-14.13,20.16-14.13,14.94,0,20.25,7,20.25,17.19h-9c-.36-6-3.06-9.72-11.52-9.72-7.74,0-10.35,2.88-10.35,5.85,0,8.9,32,2.79,32,20.06C300.61,301.91,294.94,309.11,280.36,309.11Z"/><path class="cls-1" d="M317.71,270.15v25.1c0,4.32,1.8,6.3,11.79,5.85v7.74c-15.21.9-21.6-2.7-21.6-13.59v-25.1h-6.57v-5.76l6.57-2V250.89h9.54v11.52H329.5v7.74Z"/><path class="cls-1" d="M369.64,308.48l-2-6.93c-2.16,3.6-8.55,7.56-18.09,7.56-9.9,0-17.82-4.41-17.82-14.13,0-8.46,6-12.33,19.17-13.58L365.41,280c-.27-6.66-2.7-10.53-12-10.53-8.55,0-10.89,3.42-10.89,9.17l-9.36.1c0-9.54,4-16.92,20.16-16.92,18.81,0,21.51,8.1,21.51,20.51v26.19Zm-4.23-21.24-13.05,1.44c-8,.9-10.71,2.61-10.71,6.21,0,4.23,3.6,6.66,10.35,6.66,8.46,0,13.41-4.86,13.41-12.87Z"/><path class="cls-1" d="M394.21,270.15v25.1c0,4.32,1.8,6.3,11.79,5.85v7.74c-15.21.9-21.6-2.7-21.6-13.59v-25.1h-6.57v-5.76l6.57-2V250.89h9.54v11.52H406v7.74Z"/><path class="cls-1" d="M431.65,309.11c-18.54,0-23.13-11.43-23.13-23.76s4.68-23.57,23-23.57c17,0,21.78,13,20.61,27.44H418.51c.54,7.92,4.5,12.51,12.78,12.51,7.65,0,10.08-3.69,11-7.38h10.08C451.54,302.18,446.14,309.11,431.65,309.11Zm-1-39.86c-9,0-11.61,5.58-12.06,12.33h23.67s.09,0,0-.1.09-.08,0-.08C442.36,275.1,439.39,269.25,430.66,269.25Z"/><circle class="cls-2" cx="280" cy="280" r="275"/></svg>
BIN +16.3 KB logo/logo.png
Binary file not shown.
@@ -0,0 +1 @@
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 560 560"><defs><style>.cls-1{fill:#ed1e79;}.cls-2{fill:none;stroke:#ed1e79;stroke-miterlimit:10;stroke-width:10px;}</style></defs><title>Artboard 1</title><path class="cls-1" d="M130.6,309.11c-18.18,0-22.95-11.43-22.95-23.67s4.95-23.66,22.77-23.66c14,0,20.7,7.47,20.7,19h-9.81c-.63-7.11-3.51-11.16-11.16-11.16-9.9,0-12.69,6.84-12.69,15.74,0,8.73,2.7,15.48,12.87,15.48,7.65,0,10.35-3.78,11-10.44h9.81C151.12,301.73,144.73,309.11,130.6,309.11Z"/><path class="cls-1" d="M179.11,309.11c-18.09,0-22.77-11.43-22.77-23.67s4.77-23.66,22.86-23.66c17.91,0,22.86,11.52,22.86,23.66C202.06,297.77,197.11,309.11,179.11,309.11Zm.09-39.5c-10.71,0-13,7.2-13,15.83s2.34,15.84,13,15.84c10.8,0,13.14-6.93,13.14-15.84C192.25,276.9,190.09,269.61,179.2,269.61Z"/><path class="cls-1" d="M242.38,308.48V282.83c0-8.18-1.44-13-11.25-13s-11.88,4.77-11.88,13v25.65h-9.63V262.41h5.22l2.16,6.48c1.53-3.15,6.39-7.11,16.83-7.11,15.12,0,18.09,8.82,18.09,21.87v24.83Z"/><path class="cls-1" d="M280.36,309.11c-15.57,0-22.23-6.12-22.23-17.82h9.45c0,6.3,3.6,10.17,12.6,10.17,8.19,0,10.8-3.06,11-5.76.27-10-32.13-3.33-32.13-19.79,0-8.1,5.58-14.13,20.16-14.13,14.94,0,20.25,7,20.25,17.19h-9c-.36-6-3.06-9.72-11.52-9.72-7.74,0-10.35,2.88-10.35,5.85,0,8.9,32,2.79,32,20.06C300.61,301.91,294.94,309.11,280.36,309.11Z"/><path class="cls-1" d="M317.71,270.15v25.1c0,4.32,1.8,6.3,11.79,5.85v7.74c-15.21.9-21.6-2.7-21.6-13.59v-25.1h-6.57v-5.76l6.57-2V250.89h9.54v11.52H329.5v7.74Z"/><path class="cls-1" d="M369.64,308.48l-2-6.93c-2.16,3.6-8.55,7.56-18.09,7.56-9.9,0-17.82-4.41-17.82-14.13,0-8.46,6-12.33,19.17-13.58L365.41,280c-.27-6.66-2.7-10.53-12-10.53-8.55,0-10.89,3.42-10.89,9.17l-9.36.1c0-9.54,4-16.92,20.16-16.92,18.81,0,21.51,8.1,21.51,20.51v26.19Zm-4.23-21.24-13.05,1.44c-8,.9-10.71,2.61-10.71,6.21,0,4.23,3.6,6.66,10.35,6.66,8.46,0,13.41-4.86,13.41-12.87Z"/><path class="cls-1" d="M394.21,270.15v25.1c0,4.32,1.8,6.3,11.79,5.85v7.74c-15.21.9-21.6-2.7-21.6-13.59v-25.1h-6.57v-5.76l6.57-2V250.89h9.54v11.52H406v7.74Z"/><path class="cls-1" d="M431.65,309.11c-18.54,0-23.13-11.43-23.13-23.76s4.68-23.57,23-23.57c17,0,21.78,13,20.61,27.44H418.51c.54,7.92,4.5,12.51,12.78,12.51,7.65,0,10.08-3.69,11-7.38h10.08C451.54,302.18,446.14,309.11,431.65,309.11Zm-1-39.86c-9,0-11.61,5.58-12.06,12.33h23.67s.09,0,0-.1.09-.08,0-.08C442.36,275.1,439.39,269.25,430.66,269.25Z"/><circle class="cls-2" cx="280" cy="280" r="275"/></svg>
Oops, something went wrong.

0 comments on commit 93b7b5b

Please sign in to comment.