Skip to content

Commit d0672ff

Browse files
author
Liuhaoran Huang
committed
add readme
1 parent 9d28e2a commit d0672ff

File tree

1 file changed

+87
-1
lines changed

1 file changed

+87
-1
lines changed

README.md

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,95 @@
11
# React combine react
22

3+
![npm](https://img.shields.io/npm/v/react-combine-provider.svg)
4+
[![Coverage Status](https://coveralls.io/repos/github/hlhr202/React-Combine-Provider/badge.svg?branch=master)](https://coveralls.io/github/hlhr202/React-Combine-Provider?branch=master)
5+
[![CircleCI](https://circleci.com/gh/hlhr202/React-Combine-Provider.svg?style=shield)](https://circleci.com/gh/hlhr202/React-Combine-Provider)
6+
![TypeDefine](https://img.shields.io/npm/types/chalk.svg)
7+
38
Combine react providers in ease
9+
Fully support [unstated-next](https://github.com/jamiebuilds/unstated-next) and [constate](https://github.com/diegohaz/constate)
410

511
## Install
612

713
```
8-
npm start # or yarn start
14+
npm install --save react-combine-provider
15+
```
16+
17+
## Usage
18+
19+
- unstated
20+
21+
```jsx
22+
import React, { useState } from 'react';
23+
import ReactDOM from 'react-dom';
24+
import { createContainer } from 'unstated-next';
25+
import { combineProviders } from 'react-combine-reducer';
26+
27+
const useCounter1 = (initialState = 1) => {
28+
const [count, setCount] = useState(initialState);
29+
const decrement = () => setCount(count - 1);
30+
const increment = () => setCount(count + 1);
31+
return { count, decrement, increment };
32+
};
33+
34+
const Counter1 = createContainer(useCounter1);
35+
36+
const useCounter2 = (initialState = 2) => {
37+
const [count, setCount] = useState(initialState);
38+
const decrement = () => setCount(count - 2);
39+
const increment = () => setCount(count + 2);
40+
return { count, decrement, increment };
41+
};
42+
43+
const Counter2 = createContainer(useCounter2);
44+
45+
function CounterDisplay1() {
46+
const counter1 = Counter1.useContainer();
47+
console.log('rendering');
48+
return (
49+
<div>
50+
<div>counter display 1</div>
51+
<div>counter 1</div>
52+
<button onClick={counter1.decrement}>-</button>
53+
<span>{counter1.count}</span>
54+
<button onClick={counter1.increment}>+</button>
55+
<br />
56+
</div>
57+
);
58+
}
59+
60+
function CounterDisplay2() {
61+
const counter1 = Counter1.useContainer();
62+
const counter2 = Counter2.useContainer();
63+
return (
64+
<div>
65+
<div>counter display 2</div>
66+
<div>counter 1</div>
67+
<button onClick={counter1.decrement}>-</button>
68+
<span>{counter1.count}</span>
69+
<button onClick={counter1.increment}>+</button>
70+
<div>counter 2</div>
71+
<button onClick={counter2.decrement}>-</button>
72+
<span>{counter2.count}</span>
73+
<button onClick={counter2.increment}>+</button>
74+
<br />
75+
</div>
76+
);
77+
}
78+
79+
const StateProviders = combineProviders([
80+
[Counter1.Provider, { initialState: 5 }],
81+
Counter2.Provider,
82+
]);
83+
84+
function App() {
85+
return (
86+
<StateProviders>
87+
<CounterDisplay1 />
88+
<br />
89+
<CounterDisplay2 />
90+
</StateProviders>
91+
);
92+
}
93+
94+
ReactDOM.render(<App />, document.getElementById('root'));
995
```

0 commit comments

Comments
 (0)