Giter Site home page Giter Site logo

redux-grapes's Introduction

redux-grapes

创建actions

创建3个action分别为actionIdTest,actionNoParamTest,actionTestSaga,其中第一个action包含参数id。 所有的action的type都包含前缀PREFIX。

import { createActions } from 'redux-grapes'

export default createActions({
  actionIdTest: id => ({ id }),
  actionNoParamTest:()=>({}),
  actionTestSaga: () => ({}),
}, 'PREFIX')

创建reducers

创建reducer处理对应的action,此例中为处理actionIdTest:

import { createReducers } from 'redux-grapes'
import appActions from '../actions/appActions'

const initialState = {
  testId: '',
  testName: ''
}

export default createReducers(on => {
  on(appActions.actionIdTest, (state, action) => {
    const { id } = action.payload
    return {
      ...state,
      testId: id
    }
  })
}, initialState)

创建对应的saga

创建saga来处理saga后缀的action (actionTestSaga)

mport { SagaIterator } from 'redux-saga'
import { takeLatest, put } from 'redux-saga/effects'
import appActions from '../actions/appActions'

export function* actionTest1(): SagaIterator {
  yield takeLatest(appActions.actionTestSaga, function*(action: SagaAction) {
    const { deferred } = action
    yield put(appActions.actionIdTest('id-demo'))
    yield put(appActions.actionNameTest('name-demo'))
    deferred.resolve()
  })
}

配置redux中间件

配置对应中间件将action转换为promise的中间件。

import createSagaMiddleware from 'redux-saga'
import { sagaPromiseMiddleware } from 'redux-grapes'

……

const middlewares = [
  sagaPromiseMiddleware,
  sagaMiddleWare
]

const store = createStore(rootReducer, applyMiddleware(...middlewares))

……

redux-grapes's People

Contributors

beforegolive avatar

Stargazers

 avatar  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.