|
1 | 1 | # React combine react
|
2 | 2 |
|
| 3 | + |
| 4 | +[](https://coveralls.io/github/hlhr202/React-Combine-Provider?branch=master) |
| 5 | +[](https://circleci.com/gh/hlhr202/React-Combine-Provider) |
| 6 | + |
| 7 | + |
3 | 8 | Combine react providers in ease
|
| 9 | +Fully support [unstated-next](https://github.com/jamiebuilds/unstated-next) and [constate](https://github.com/diegohaz/constate) |
4 | 10 |
|
5 | 11 | ## Install
|
6 | 12 |
|
7 | 13 | ```
|
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')); |
9 | 95 | ```
|
0 commit comments