Permalink
Browse files

Init commit

  • Loading branch information...
jamiebuilds committed Jan 31, 2018
0 parents commit cf98bd830caada495f89580ee43be2e7ee1982af
Showing with 4,031 additions and 0 deletions.
  1. +16 −0 .babelrc
  2. +11 −0 .flowconfig
  3. +3 −0 .gitignore
  4. +3 −0 .prettierrc
  5. +11 −0 .travis.yml
  6. +19 −0 LICENSE
  7. +122 −0 README.md
  8. +74 −0 example/complex.js
  9. +38 −0 example/simple.js
  10. +41 −0 package.json
  11. +100 −0 src/index.js
  12. +4 −0 test.js
  13. +3,589 −0 yarn.lock
@@ -0,0 +1,16 @@
{
"presets": [
[
"env",
{
"loose": true,
"targets": {
"browsers": ["last 1 version"]
}
}
],
"flow",
"react"
],
"plugins": ["transform-class-properties"]
}
@@ -0,0 +1,11 @@
[ignore]

[include]

[libs]

[lints]

[options]

[strict]
@@ -0,0 +1,3 @@
node_modules
*.log
lib
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
@@ -0,0 +1,11 @@
git:
depth: 1
sudo: false
language: node_js
node_js:
- '8'
cache:
yarn: true
directories:
- node_modules
script: yarn test && yarn flow
19 LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2018-present James Kyle <me@thejameskyle.com>

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.
122 README.md
@@ -0,0 +1,122 @@
> **Important!** This library is not actually implemented. It's just a detailed
> idea. If you want to implement it, just clone the repo and go for it.
# Unstated

> State ain't shit
## Installation

```sh
yarn add unstated
```

## Example

```js
import React from 'react';
import { render } from 'react-dom';
import { Subscribe, Container } from 'unstated';
type CounterState = {
count: number
};
class CounterContainer extends Container<CounterState> {
state = {
count: 0
};
increment() {
this.setState({ count: this.state.count + 1 });
}
decrement() {
this.setState({ count: this.state.count - 1 });
}
}
function Counter() {
return (
<Subscribe to={[CounterContainer]}>
{counter => (
<div>
<button onClick={counter.decrement}>-</button>
<span>{counter.state.count}</span>
<button onClick={counter.increment}>+</button>
</div>
)}
</Subscribe>
);
}
render(<Counter />, document.getElementById('root'));
```

## API

### `Container`

`Container` is a base class for you to extend with your own methods and state.
The API is designed to look just like React Component's except it doesn't have
all the stuff related to rendering.

```js
class CounterContainer extends Container {
state = { count: 0; };
increment() {
this.setState({ count: this.state.count + 1 });
}
}
```

Just like a `React.Component` class, you can have a `state` property which is
an object with whatever you want inside.

Inside of your methods you can call `setState(nextState)` whenever you want. It
works just like React's `setState` method.

### `<Subscribe>`

`Subscribe` is a component for adding states to your React tree.

```js
<Subscribe to={[CounterContainer]}>
{counter => (
...
)}
</Subscribe>
```

You pass the containers you want to subscribe to into the `to` property. Then
in your children function (render prop) you'll receive instances of your
containers.

These instances might come from a parent component, otherwise it will create
its own instances. These instances will then be passed through React's context
to any child components.

##### `<Provide>`

Unstated will create its own instances of your containers internally. But if
you want to provide your own instances (perhaps for dependency injection in
tests), you can do that with `<Provide>`.

```js
let counter = new CounterContainer();
let tree = render(
<Provide inject={[counter]}>
<Counter />
</Provide>
);
click(tree, '#increment');
assert(counter.state.count === 1);
click(tree, '#increment');
assert(counter.state.count === 2);
```

`<Provide>` accepts `inject` property which should be an array of container
instances to be passed to all the children.
@@ -0,0 +1,74 @@
// @flow
import React from 'react';
import { render } from 'react-dom';
import { Subscribe, Container } from '../src';

type AppState = {
amount: number
};

class AppContainer extends Container<AppState> {
state = {
amount: 1
};

setAmount(amount: number) {
this.setState({ amount });
}
}

type CounterState = {
count: number
};

class CounterContainer extends Container<CounterState> {
state = {
count: 0
};

increment(amount: number) {
this.setState({ count: this.state.count + amount });
}

decrement(amount: number) {
this.setState({ count: this.state.count - amount });
}
}

function Counter() {
return (
<Subscribe to={[AppContainer, CounterContainer]}>
{(app, counter) => (
<div>
<span>Count: {counter.state.count}</span>
<button onClick={() => counter.decrement(app.state.amount)}>-</button>
<button onClick={() => counter.increment(app.state.amount)}>+</button>
</div>
)}
</Subscribe>
);
}

function App() {
return (
<Subscribe to={[AppContainer]}>
{app => (
<div>
<Counter />
<label>Amount: </label>
<input
type="number"
value={app.state.amount}
onChange={event => {
app.setCounterAmount(event.currentTarget.value);
}}
/>
</div>
)}
</Subscribe>
);
}

let root = document.getElementById('root');
if (!root) throw new Error('Missing #root');
render(<App />, root);
@@ -0,0 +1,38 @@
// @flow
import React from 'react';
import { render } from 'react-dom';
import { Subscribe, Container } from '../src';

type CounterState = {
count: number
};

class CounterContainer extends Container<CounterState> {
state = { count: 0 };

increment() {
this.setState({ count: this.state.count + 1 });
}

decrement() {
this.setState({ count: this.state.count - 1 });
}
}

function Counter() {
return (
<Subscribe to={[CounterContainer]}>
{counter => (
<div>
<button onClick={counter.decrement}>-</button>
<span>{counter.state.count}</span>
<button onClick={counter.increment}>+</button>
</div>
)}
</Subscribe>
);
}

let root = document.getElementById('root');
if (!root) throw new Error('Missing #root');
render(<Counter />, root);
@@ -0,0 +1,41 @@
{
"name": "unstated",
"version": "0.0.0",
"description": "State ain't shit",
"main": "lib/index.js",
"repository": "https://github.com/thejameskyle/unstated",
"author": "James Kyle <me@thejameskyle.com>",
"license": "MIT",
"files": ["lib"],
"scripts": {
"build": "babel src -d lib",
"typecheck": "flow",
"test": "ava",
"format": "prettier --write **/*.{js,json,md}",
"prepublish": "yarn run build",
"precommit": "lint-staged"
},
"peerDependencies": {
"create-react-context": "^0.1.4",
"react": "^15.0.0 || ^16.0.0",
"react-dom": "^15.0.0 || ^16.0.0"
},
"devDependencies": {
"ava": "^0.25.0",
"babel-cli": "^6.26.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.6.1",
"babel-preset-flow": "^6.23.0",
"babel-preset-react": "^6.24.1",
"create-react-context": "^0.1.4",
"flow-bin": "^0.64.0",
"husky": "^0.14.3",
"lint-staged": "^6.1.0",
"prettier": "^1.10.2",
"react": "^16.2.0",
"react-dom": "^16.2.0"
},
"lint-staged": {
"*.{js,json,md}": ["prettier --write", "git add"]
}
}
Oops, something went wrong.

0 comments on commit cf98bd8

Please sign in to comment.