Skip to content

Commit 450ba99

Browse files
committed
update reademe
1 parent 90b3b93 commit 450ba99

File tree

1 file changed

+14
-209
lines changed

1 file changed

+14
-209
lines changed

README.md

Lines changed: 14 additions & 209 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
[🇨🇳 中文](https://github.com/reactive-react/xreact/wiki/%E6%95%99%E7%A8%8B) | [🌰 Examples](examples) | [🎓 Best Practices](https://github.com/reactive-react/xreact/wiki/frp-best-practice) | [ 📖 Wiki](https://github.com/reactive-react/xreact/wiki)
1+
# xReact
22

3-
# xreact
4-
5-
A Functional Reactive State Wrapper for React Components
3+
xReact is a Functional Reactive State Wrapper for React Components. Data flow in xReact is observable and unidirectional.
64

75
> formerly know as react-most, renamed so because mostjs is not madatory anymore.
86
@@ -13,221 +11,28 @@ A Functional Reactive State Wrapper for React Components
1311
[![npm](https://img.shields.io/npm/v/xreact.svg)](https://www.npmjs.com/package/xreact)
1412
[![greenkeeper.io](https://badges.greenkeeper.io/reactive-react/xreact.svg)](https://greenkeeper.io)
1513

16-
## Install
17-
```
18-
npm install xreact --save
19-
# or
20-
yarn add xreact
21-
```
22-
23-
## What
24-
`xreact` is a lightweight Higher Order State Component for React.
25-
26-
Data flow in `xreact` is simple and unidirectional, similar to flux.
27-
28-
![](https://www.evernote.com/l/ABcSUEkq5_xPTrWy_YdF5iM1Fxu14WMB7eAB/image.png)
29-
30-
## Terminology
31-
- **Machine**: a machine can emit `Update` to a timeline `update$`, and can be operated by calling function in `actions`
32-
- **Plan**: a Plan is a function that describe how to create a `Machine`
33-
- **Update**: a function of `currentState -> nextState`
34-
- **Action**: a function that create instance of `Intent`
35-
- **Intent**: describe what you want to do
36-
- **Intent Stream**: a timeline of every `Intent` created by every `Action`
37-
38-
## Quick Start
39-
40-
sorry we don't have a **book** to document how to use `xreact`, and I don't really need to,
41-
there's only 3 things you should notice when using `xreact`, I'll explain by a simple counter app.
42-
43-
Also, you can refer more documents here:
44-
45-
46-
47-
### 1. Define a simple stateless View component
48-
49-
![](https://www.evernote.com/l/ABd-YTQc2FVBjqOEkpiFZDltPloti8a2Hq8B/image.png)
50-
51-
```html
52-
const CounterView = ({actions, count}) => (
53-
<div>
54-
<button onClick={actions.dec}>-</button>
55-
<span>{count}</span>
56-
<button onClick={actions.inc}>+</button>
57-
</div>
58-
)
59-
```
60-
61-
every View component expected a `actions` fields in `props`
62-
63-
### 2. Define a `Plan`
64-
65-
![](https://www.evernote.com/l/ABeLlbr3vQNM_JKfcd_W4zfW262lxWJhOsMB/image.png)
66-
67-
After we have a pretty view for represention and inteacting interface, we can define how to update the view, or "how to react on actions". In such case:
68-
69-
1. A counter can have actions of `inc` and `dec`, which will send `Intent` of `{type: 'inc'}` or `{type:'dec'}` to `Intent Stream` upon being called.
70-
2. A counter reactively generates `Update` when it receives an `Intent` of either type `inc` or `dec`.
71-
72-
```js
73-
const plan = (intent$) => {
74-
return {
75-
update$: intent$.map(intent => {
76-
switch (intent.type) {
77-
case 'inc':
78-
return state => ({ count: state.count + 1 });
79-
case 'dec':
80-
return state => ({ count: state.count - 1 });
81-
default:
82-
return _ => _;
83-
}
84-
}),
85-
actions: {
86-
inc: () => ({ type: 'inc' }),
87-
dec: () => ({ type: 'dec' })
88-
}
89-
}
90-
}
9114
```
92-
a `Plan` will take `intent$`(Intent Stream) and return a `Machine`.
93-
94-
a `Machine` defines
95-
96-
- how you can act on the machine
97-
- how the machine will react on intents.
98-
99-
### 3. Plan X View
10015
101-
![](https://www.evernote.com/l/ABdv2Ks5f7dNQKxyoz7Q1eB9Xm9vy3U11ZMB/image.png)
10216
103-
```js
104-
import {render} from 'react-dom'
105-
import X, {x} from 'xreact/lib/x'
106-
import * as rx from 'xreact/lib/xs/rx'
17+
## Get Started
10718
108-
const Counter = x(plan)(CounterView)
19+
xReact works for both TypeScript and CMD JS, to install xReact simply use you yarn or npm:
10920
110-
render(
111-
<X x={rx}>
112-
<Counter />
113-
</X>,
114-
document.getElementById('app')
115-
);
11621
```
117-
`Counter` is product(x) of `plan` and `CounterView`, which means it can react to `Intent` as it's plan, and update `CounterView`
118-
119-
`<X></X>` will provide a `intent$` instance.
120-
121-
122-
## Features
123-
Inspired by Redux and Functional Reactive Programming, `xreact` allows you to model user events, actions, and data as reactive streams. Now you can map, filter, compose, and subscribe those streams into your application's state.
124-
125-
### Purely Functional, Declarative, and Monadic
126-
In imperatively written code, you describe step-by-step how to process data. With `xreact`, we simply define data transformations, then compose them to form our data flow. There are no variables, no intermediate state, and no side effects in your data flow's data composition!
127-
128-
### Fantasy Land<sup>beta</sup>
129-
130-
![](https://github.com/fantasyland/fantasy-land/raw/master/logo.png)
131-
132-
There's also [Fantasy Land](https://github.com/fantasyland/fantasy-land) implementation in `src/fantasy.ts`, you can see [bmi calculator example](https://github.com/reactive-react/xreact/blob/master/examples/bmi-calc/app.tsx) to get the idea.
133-
134-
### Typesafe and scalable
135-
Rewritten in Typescript, and abstract Stream as Higher Kind type so easier to bring new FRP lib to integrete with xreact.
136-
137-
### Composable and Reusable `Plan`
138-
In Redux, reducers' use of `switch` statements can make them difficult to compose. Unlike reducers, the function `x` return is simply a function which can easily compose.
139-
140-
141-
```js
142-
const Counter = x(plan1)(x(plan2)(CounterView))
143-
// is the same as
144-
const plan1_x_plan2_x = compose(x(plan1), x(plan2))
145-
const Counter = plan1_x_plan2_x(CounterView)
146-
```
147-
148-
what really happen behind compose is actually ES6 style mixin, so there won't be any extra layer of HoC and no any performance overhead.
149-
150-
### Easy to test, no need for mocks
151-
Because UI and UI behavior are loosely coupled, you can test a React component by just passing it data. Behaviors can be tested by calling actions and then verifying the state.
152-
153-
```js
154-
import {mount} from 'enzyme'
155-
import * as rx from 'xreact/lib/xs/rx'
156-
import {rx as xtest} from 'xreact/lib/xtests'
157-
const mountx = compose(mount, c => React.createFactory(X)({ x: rx }, c))
158-
159-
let counterX = mountx(
160-
<Counter />
161-
)
162-
163-
let counter = counterX.find(Counter).getNode()
164-
let counterView = counterX.find(CounterView)
165-
let actions = counterView.prop('actions')
166-
it('add intent to intent$ and go through sink$', () => {
167-
return new xtest
168-
.do([
169-
actions.inc,
170-
actions.inc,
171-
actions.inc,
172-
])
173-
.collect(counter)
174-
.then(state => expect(state.count).toBe(3))
175-
})
176-
177-
```
178-
179-
see more details about testing examples at <https://github.com/reactive-react/xreact/blob/master/src/__tests__/xtest.tsx>
180-
181-
### Async actions made easy
182-
Asynchronous functions, such as Promises, can be converted to a stream and then flat-mapped.
183-
184-
```js
185-
import {Observable} from '@reactivex/rxjs/Observable'
186-
187-
...
188-
189-
intent$.filter(x=>x.kind=='rest')
190-
.flatMap(({url}) => Observable.fromPromise(rest(url)))
191-
.map(...)
192-
193-
...
22+
npm install xreact --save
23+
# or
24+
yarn add xreact
19425
```
19526
196-
see `rest(url)` will return a `Promise`,
197-
198-
### Higher level extract and ready for any FRP library
199-
xreact came with 2 FRP libs of choice, rxjs and mostjs, for any new lib you only need to implement the `StaticStream` with your prefered lib as Higher Kind Type.
200-
201-
more details about HKT implementation in TypeScript is [here](https://github.com/gcanti/fp-ts)
202-
203-
## FAQ
204-
205-
### How it's different from redux?
206-
207-
unlike redux, xreact turn FRP to 11 in react, it model problem different
208-
209-
- "global" intent stream(using redux's word should be intent store) not global state store
210-
- there's not such thing as state store, no state will store anywhere, only state transformations
211-
- FRP lib as your choice, choose any lib your familiar with
212-
213-
### How it's different from cycle.js?
214-
215-
think xreact as a more specify and optimized cycle just for react.
27+
- Quick Start :point_right: https://xreact.oyanglul.us/Get-Started.html
28+
- Examples :point_right: https://github.com/reactive-react/xreact/tree/master/examples
21629
217-
### Why not global state?
218-
global is state is not scalable, think it as a database, and every component query data from it,however, database are hard to scale, design and maintain.
30+
## Documentation
21931
220-
instead of making state global, we think a better choice of doing such reversely, just have what you want to do(intent) globally instead. So, every component can just broadcast what it's trying to do, but only focus on how to reduce intent into a state transformation for it self.
32+
xReact information and documentation is available on [the website](http://xreact.oyanglul.us/).
22133
222-
In this case, one component won't need worry about how the global state structure, and just focus on itself. So, components are more modular and decoupled.
34+
<!-- We also have a Scaladoc index. -->
22335
224-
Furher more, it's composable, we can build small x component constructors and compose them at will to create a bigger and powerfult component constructors. It's much easier and flexible by compose small behavior and state into a big component, not destruct a big global state into small components.
36+
Finally, we have a list of [frequently-asked questions](https://xreact.oyanglul.us/FAQ.html).
22537
226-
## Thanks to...
227-
- [rxjs](https://github.com/ReactiveX/rxjs)
228-
- [most](https://github.com/cujojs/most)
229-
- [React](http://facebook.github.io/react/)
230-
- [redux](https://github.com/rackt/redux)
231-
- [Om](https://github.com/omcljs/om)
232-
- [Cycle](http://cycle.js.org/)
233-
- [transdux](https://github.com/jcouyang/transdux)
38+
Our goal is to have clear and comprehensive documentation. If you notice problems, omissions, or errors, please [let us know](https://github.com/reactive-react/xreact/issues).

0 commit comments

Comments
 (0)