# redux-bridge

> redux与组件的简单桥接工具

Latest version **1.0.6** (published 2020-05-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-bridge
pnpm add redux-bridge
yarn add redux-bridge
bun add redux-bridge
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2020-05-16 |
| First published | 2020-05-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 19.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | anye931123 |
| Maintainers | anye931123 |
| Keywords | hooks, state, redux |

## Links

- npm: https://www.npmjs.com/package/redux-bridge
- Repository: https://github.com/anye931123/redux-bridge
- Homepage: https://github.com/anye931123/redux-bridge#readme
- Issues: https://github.com/anye931123/redux-bridge/issues
- npm.io page: https://npm.io/package/redux-bridge

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 1.0.6 (latest) — 2020-05-16
- 1.0.5 — 2020-05-11
- 1.0.4 — 2020-05-11
- 1.0.3 — 2020-05-10
- 1.0.2 — 2020-05-10
- 1.0.1 — 2020-05-10
- 1.0.0 — 2020-05-10

## README

# redux-bridge
Redux与组件之间的桥梁，声明式获取state，使用简单，通过useSelector获取store中你需要的一个或者一组state，通过controlUpdate回调方法，实现shouldComponentUpdate内控渲染。

[![npm package](https://img.shields.io/npm/v/redux-bridge.svg?style=flat-square)](https://www.npmjs.org/package/redux-bridge)
## Installation
```sh
npm install redux-bridge  --save
// or
yarn add redux-brige
```
## usage
```jsx
import React from 'react';
import ReactDOM from 'react-dom';
import { createStore } from 'redux';
import {Provider,useSelector,useDispatch} from 'redux-bridge';


// 1️⃣ Create the store
const reducer=(state,action)=> {
    switch (action.type) {
        case 'add':
            return {
                count: ++state.count
            }
        case 'subtract':
            return {
                count: --state.count
            }
        default:
            return state
    }

}
const store = createStore(reducer,{count:0});



function Counter() {
    const {count} = useSelector(['count']);
    const dispatch = useDispatch()
    return (
        <div>
            <span>{count}</span>
            <button type="button" onClick={()=>dispatch({type:'add'})}>+</button>
            <button type="button" onClick={()=>dispatch({type:'subtract'})}>-</button>
        </div>
    );
}

function DoubleCounter() {
    const {count} = useSelector(['count'],(prevState,nextState)=>{
        return nextState.count%2===0
    });
    return (
        <div>
            <span>{count}</span>
        </div>
    );
}



// 4️⃣ Wrap your components with Provider
function App() {
    return (
        <Provider store={store}>
            <Counter />
            <DoubleCounter />
        </Provider>
    );
}

const rootElement = document.getElementById('root');
ReactDOM.render(<App />, rootElement);

```
## Api
### Provider
`Provider(props: { children,store,context })`
将 store 和 React 应用进行绑定。

```jsx
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'redux-bridge';

ReactDOM.render(
  <Provider>
    <App />
  </Provider>,
  rootEl
); 
```
|属性|类型|说明|
|--:|--:|--:|
|store|Store|通过redux createStore生成的store供应用消费|
|context|Context|react Context，如果使用多store的情况，一般为三方库使用，需要指定自己的Context，以防与其他应用冲突|

### useSelector
`useSelector<T>(selector: string[], controlUpdate?: ControlUpdate<T>, stateDeep?: string)`

状态选择器，通过声明好的state 属性名数组，获取一组states

##### selector 声明状态选择器
```
//store中的状态树
{
a:1,
b:2,
c:3,
d:{
  e:5,
  f:6,
  g:{h:7}
  }
}
//获取想要的状态
const{a,b}= useSelector(['a','b'])
```
通过设置 selector 从store中获取你想要的状态
##### controlUpdate 控制更新类似shouldComponentUpdate
```
const{a,b}= useSelector(['a','b'],(preState,nextState)=>{
return preState.a!==nextState.a||preState.b!==nextState.b
})
```
如果返回`true`更新组件`false`不更新组件

##### stateDeep 状态深度
```
const{e,f}= useSelector(['e','f'],undefined，'d')
const {h}=useSelector(['h'],undefined,'d.g')
```
### useDispatch 获取redux dispatch
```jsx
const dispatch=useDispatch()
```

---
_Source: https://npm.io/package/redux-bridge · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
