Skip to content

Commit 6386da9

Browse files
authored
Merge branch 'master' into greenkeeper/initial
2 parents c8d6eac + cdb6922 commit 6386da9

16 files changed

Lines changed: 190 additions & 1775 deletions

.babelrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"presets": ["@babel/preset-env", "@babel/preset-react"],
3-
"plugins": ["@babel/plugin-proposal-class-properties", "@babel/plugin-proposal-object-rest-spread"],
43
"sourceMaps": "inline",
54
"retainLines": true
65
}

README.md

Lines changed: 33 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -2,176 +2,64 @@
22

33
[![Build Status](https://travis-ci.org/microstates/react.svg?branch=master)](https://travis-ci.org/microstates/react) [![Greenkeeper badge](https://badges.greenkeeper.io/microstates/react.svg)](https://greenkeeper.io/)
44

5-
Component for rendering Microstates models in React. Go to [Microstates](https://github.com/cowboyd/microstates.js) repo to learn about Microstates models.
5+
This package provides `useType` hook for React that supports `useState` hook.
6+
7+
*If your version of React doesn't have `useState`, don't install this package. Go to [Microstates with React Class Components](#microstates-with-react-class-components) section.*
68

79
## Installation
810

911
```bash
1012
npm install --save @microstates/react
11-
```
1213

13-
or
14+
# or
1415

15-
```bash
1616
yarn add @microstates/react
1717
```
1818

19-
## How to use
20-
21-
This library provides a component that takes a Microstates model type and creates an instance of the given type. The instance will be sent to one the following: `children` function, props `render` function or a context consumer.
22-
23-
### <State type={Type} value={any} render={fn} from={any} />
24-
25-
State component takes type and value arguments. Type is a class definition that describes the structure of the data. The component will instantiate this class by passing it to `Microstates.create(Type, value)`. The `value` prop is used to provide initial value for the Microstates model.
19+
## Usage
2620

27-
### `children` function
28-
29-
```js
30-
import State from "@microstates/react";
21+
`useType` takes the same arguements as Microstates `create` but it provides you with a microstate
22+
that will re-render the component on every transition.
3123

24+
```jsx
3225
function App() {
33-
return (
34-
<State type={Number} value={42}>
35-
{number => {
36-
return (
37-
<div>
38-
<span className="value">{number.state}</span>
39-
<button onClick={() => number.increment()}>Increment</button>
40-
</div>
41-
);
42-
}}
43-
</State>
44-
);
45-
}
46-
```
47-
48-
### props `render` function
49-
50-
If you prefer props `render` function, you can use it same as the `children` function.
51-
52-
```js
53-
import State from "@microstates/react";
54-
55-
function App() {
56-
return (
57-
<State
58-
type={Number}
59-
value={42}
60-
render={number => {
61-
return (
62-
<div>
63-
<span className="value">{number.state}</span>
64-
<button onClick={() => number.increment()}>Increment</button>
65-
</div>
66-
);
67-
}}
68-
/>
69-
);
70-
}
71-
```
72-
73-
### `from` prop
26+
let number = useType(Number, 42);
7427

75-
`from` prop allows you to create a microstate by providing an initial value for the microstate without providing a Type. Microstates will figure out the transitions and state from the value itself.
76-
77-
```js
78-
import Microstates from "@microstates/react";
79-
80-
function App() {
8128
return (
82-
<State
83-
from={42}
84-
render={number => {
85-
return (
86-
<div>
87-
<span className="value">{number.state}</span>
88-
<button onClick={() => number.increment()}>Increment</button>
89-
</div>
90-
);
91-
}}
92-
/>
29+
<>
30+
<span>{number.state}</span>
31+
<button onClick={() => number.increment()}>Increment</button>
32+
</>
9333
);
9434
}
9535
```
9636

97-
### Context API
37+
## Microstates with React Class Components
9838

99-
`Microstates` component has a Context Provider that is compatible with [React RFCs #2: New Version of Context API](https://github.com/reactjs/rfcs/blob/master/text/0002-new-version-of-context.md). It is also backwards compatible with pre React 16.3.
39+
It's easier to setup Microstates with React Class Component than to install a package
40+
that provides a component that does this. Here is how it's done.
10041

101-
To use the context API, import `Consumer` component from `@microstates/react` package.
42+
```jsx
43+
import React from 'react';
44+
import { create, Store } from 'microstates';
10245

103-
```js
104-
import State, { Consumer } from "@microstates/react";
46+
let initial = create(Number, 42);
10547

106-
class ModalModel {
107-
content = String;
108-
isOpen = Boolean;
109-
}
48+
class App extends React.Component {
49+
// this function will be invoked when transition is called
50+
// it will receive the next microstate. Set it onto your state.
51+
update = $ => this.setState({ $ });
11052

111-
class AppModel {
112-
modal = ModalModel;
113-
counter = Number;
114-
}
53+
state = {
54+
// I'm using $ cause I'm bling like that,
55+
// but you can use anything you want
56+
$: Store(initial, this.update)
57+
}
11558

116-
function Modal() {
117-
return (
118-
<Consumer>
119-
{model => {
120-
if (model.state.modal.isOpen) {
121-
return (
122-
<div className="modal-content">
123-
{model.state.modal.content}
124-
</div>
125-
)
126-
}
127-
return null;
128-
}}
129-
</Consumer>
130-
)
131-
}
132-
133-
function App() {
134-
return (
135-
<State
136-
type={AppModel}
137-
value={{ modal: { content: "Hello World!!!" }, counter: 42 }}
138-
>
139-
{model => {
140-
return (
141-
<div>
142-
<button onClick={() => model.modal.isOpen.toggle()}>Toggle Modal</button>
143-
<Modal>
144-
</div>
145-
);
146-
}}
147-
</State>
148-
);
59+
render() {
60+
return this.state.$.state;
61+
}
14962
}
15063
```
15164

152-
## onChange(nextValue: any)
153-
154-
`onChange` prop can be used to receive the serialized value of the microstate after every state transition.
155-
156-
```js
157-
import State from "@microstates/react";
158-
159-
function App() {
160-
return (
161-
<State type={Number} value={42} onChange={value => console.log(value)}>
162-
{number => {
163-
return (
164-
<div>
165-
<span className="value">{number.state}</span>
166-
<button onClick={() => number.increment()}>Increment</button>
167-
</div>
168-
);
169-
}}
170-
</State>
171-
);
172-
}
173-
```
174-
175-
## Credits
176-
177-
Big thanks to Jamie Kyle for [create-react-context](https://github.com/jamiebuilds/create-react-context) package that provides the React Context API Polyfill.
65+
That's it.

build.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

jest.config.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

package.json

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
{
22
"name": "@microstates/react",
3-
"version": "0.9.0",
4-
"description": "Microstates Connect Component for React",
3+
"version": "0.12.0-alpha.0",
4+
"description": "Provides useType hook",
55
"scripts": {
66
"build": "rollup --config",
7-
"prepare": "npm test",
8-
"test": "jest --config=jest.config.js",
7+
"prepare": "npm run build",
8+
"pretest": "npm run build",
9+
"test": "jest",
910
"start": "npm test -- --watch"
1011
},
1112
"main": "dist/@microstates/react.umd.js",
1213
"module": "dist/@microstates/react.js",
1314
"browser": "dist/@microstates/react.umd.js",
1415
"files": [
1516
"dist",
16-
"src",
17+
"use-type.js",
1718
"README.md"
1819
],
1920
"repository": {
@@ -27,36 +28,32 @@
2728
"statecharts",
2829
"finite-state-machine"
2930
],
30-
"author": "Taras Mankovski <taras@thisdot.co>",
31+
"author": "Taras Mankovski <taras@frontside.io>",
3132
"license": "MIT",
3233
"bugs": {
3334
"url": "https://github.com/microstates/react/issues"
3435
},
3536
"homepage": "https://github.com/microstates/react#readme",
3637
"devDependencies": {
3738
"@babel/core": "^7.1.2",
38-
"@babel/plugin-proposal-class-properties": "^7.1.0",
39-
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
4039
"@babel/preset-env": "^7.1.0",
41-
"@babel/preset-react": "^7.0.0",
40+
"@babel/preset-react": "7.0.0",
4241
"babel-core": "7.0.0-bridge.0",
4342
"babel-jest": "23.6.0",
44-
"enzyme": "3.7.0",
45-
"enzyme-adapter-react-16": "1.7.0",
4643
"jest": "23.6.0",
47-
"jest-cli": "^23.6.0",
48-
"microstates": "^0.12.2",
49-
"react": "16.6.3",
50-
"react-dom": "16.6.3",
51-
"rollup": "0.67.4",
52-
"rollup-plugin-babel": "^4.0.3"
44+
"microstates": "^0.12.0",
45+
"react": "16.7.0-alpha.0",
46+
"react-dom": "16.7.0-alpha.0",
47+
"rollup": "0.67.0",
48+
"rollup-plugin-babel": "^4.0.3",
49+
"@bigtest/react": "0.1.2",
50+
"@bigtest/interactor": "0.9.1"
5351
},
5452
"peerDependencies": {
55-
"microstates": "^0.11.0",
56-
"react": "^0.14.0 || ^15.0.0-0 || ^16.0.0-0"
53+
"microstates": "^0.12.0",
54+
"react": "16.7.0-alpha.0"
5755
},
58-
"dependencies": {
59-
"create-react-context": "0.2.3",
60-
"prop-types": "15.6.2"
56+
"jest": {
57+
"testRegex": "(\\.test)\\.js$"
6158
}
6259
}

rollup.config.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,18 @@ const pkg = require('./package.json');
44
const { keys } = Object;
55

66
const globals = {
7-
funcadelic: 'Funcadelic',
8-
react: 'React',
9-
'prop-types': 'PropTypes',
10-
rxjs: 'RxJS',
117
microstates: 'Microstates',
12-
'create-react-context': 'createReactContext'
8+
react: 'React'
139
};
1410

1511
module.exports = {
16-
input: 'src/index.js',
12+
input: 'use-type.js',
1713
external: keys(globals),
1814
output: [
1915
{
2016
file: pkg.browser,
2117
format: 'umd',
22-
name: 'MicrostatesReact',
18+
name: 'useType',
2319
globals,
2420
exports: 'named',
2521
sourcemap: true
@@ -30,15 +26,13 @@ module.exports = {
3026
babel({
3127
babelrc: false,
3228
comments: false,
33-
plugins: ['@babel/plugin-proposal-class-properties', '@babel/plugin-proposal-object-rest-spread'],
3429
presets: [
3530
[
3631
'@babel/preset-env',
3732
{
3833
modules: false
3934
}
40-
],
41-
'@babel/preset-react'
35+
]
4236
]
4337
})
4438
]

setupTests.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/index.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)