Giter Club home page Giter Club logo

redux-bridge's Introduction

redux-bridge

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

npm package

Installation

npm install redux-bridge  --save
// or
yarn add redux-brige

usage

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 应用进行绑定。

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

const dispatch=useDispatch()

redux-bridge's People

Contributors

anye931123 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.