4 Star 6 Fork 0

evolify / EvRedux

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

EvRedux

An easy way to use redux.

When we use Redux in React, usually we need a middleware to support async action,such as redux-thunk.

we have to write action and reducer like this:

//ActionType  R.js
export ActionType{
  	TYPE1:'type1',  
  	TYPE2:'type2',  
    TYPE3:'type3',  
    // others
}
    
//reducer reducer.js
import {ActionType as AT} from 'R'
export default function(state={},action){
  switch(action.type){
    case AT.TYPE1:
      	return {
          ...state,
          ...action.data,
          // or other
        }
    case AT.TYPE2:
          //...
    default:
    	return state      
  }      
}

//Action Action.js
import {ActionType as AT} from 'R'
const act = {
  action1:data=>dispatch=>{
    // some action such as ajax
    dispatch({
      type:AT.TYPE1,
      data:{
        // data
      }
    })
  }    
  action2:()=>(dispatch,getState)=>{
  	// call another action
  	dispatch(act.action3())
  }
  action3:()=>()=>{
    // even you never use the dispath or getState,you still have to code like this.
  }
}
export default act

// Component
@connect(
	state=>state.reducer1,
  	dispatch=>bindActionCreators(Action,dispatch)
)
export default class View extends React.Component{
    
    doAction1(){
      this.props.action1({})
    }
    
    render(){
      return(
      	<div>
          anything else
        </div>
      )
    }
}

Isn't is terible to code so many ActionType 、reduceer、dispatch and getState?

Now with EvRedux, things goes easy like this:

first install ev-redux:

npm i --save ev-redux

then you code like this:

// init evStore in you app.js when create store.
import {evStore} from 'ev-redux'
const store = createStore(...)
evStore.init(store)

// redux/reducer.js
import {ActionType as TYPE} from './index.js'
const initState={}
export default function(state=initState,action){
    if(action.type === TYPE){
        return {
            ...state,
            ...action.data
        }
    }
    return state
}

// redux/Action.js
import {evRedux} from 'ev-redux'

@evRedux
export default class Action{
    constructor(actionType){
        this.actionType = actionType
    }
    action1(x){
        this.update({
            x
        })
    }
    action2(){
        this.dispatch({
            type:this.actionType,
            data:{y:this.getState().reducer2.y+1}
        })
      	// to call another action, just call no need dispath!
      	this.action3({z:3})
        // or just update
        // this.update({y:this.getState().reducer2.y+1})
    }
    action3(z){
        setTimeout(()=>{
            this.update({z})
        },1000)
    }
}

// redux/index.js
import Action from './Action'
import reducer from './reducer'
import {connect as conn} from 'react-redux'

const connect = view => conn(state=>state.reducer2)(view)
const ActionType = Symbol()
const action = new Action(ActionType)

export {reducer,ActionType,action,connect}

// Component
import action from './action'
@connect
export default class View extends React.Component{
    
    doAction1(){
      // just call an action as we call a function.
      action.action1()
    }
    
    render(){
      return(
      	<div>
          anything else
        </div>
      )
    }
}

Isn't it amazing?

  • No need middlerware for async action.
  • No need to code so many actionTypes.
  • No need so many reducer, and just construct the state in action.
  • No need to code dispatch and getState manually, and just call action as we call an simple function.

Couldn't wait to use it? just npm i --save ev-redux

TODO

  • Simplify the reducer.
MIT License Copyright (c) 2018 evolify Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

An easy way to use redux. 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/evolify/EvRedux.git
git@gitee.com:evolify/EvRedux.git
evolify
EvRedux
EvRedux
master

搜索帮助