Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Action creator with steroids for Redux or whatever you want

NotificationsYou must be signed in to change notification settings

goncy/async-action-creator

Repository files navigation

Build StatusCoverage Status

Async actions with steroids for Redux or whatever you want.

What

Action creator is a library that helps with handling async actions on redux or similar implementations, giving a couple of methods for dispatching async actions and handling actions status, errors, and responses in a separated reducers so you don't need to hesitate creating a reducer for that.

How

// Creating an actionimport{createAction}from'async-action-creator'constmyAction=createAction('MY_ACTION')// Mounting the reducerimport{REDUCER_NAME,reducer}from'async-action-creator'constrootReducer=combineReducers({// All of your reducers[REDUCER_NAME]:reducer// Naming is important})// Creating the servicesexportdefault{[myAction.TYPE]:{action:myAction,uri:"https://api.chucknorris.io/jokes/random",method:"GET",onResolve:response=>response.value,onReject:error=>error.message,options:options=>({      ...options,'content-type':'text'})},};// Adding the middlewareimport{middleware}from'async-action-creator'...createStore(rootReducer,applyMiddleware(middleware(services)))...// Middleware in actiondispatch(myAction.run())// Actions automatically dispatched:{type:'MY_ACTION'}{type:'MY_ACTION_STARTED'}{type:'MY_ACTION_RESOLVED',payload:"Jean-Claude Van Damme once attempted to throw a Chuck Norris Roundhouse Kick. He was immediately arrested for fraud."}// Dispatching async actions manuallyconstmapDispatchToProps={run:myAction.run,// run({foo: 'bar'}) -> {type: MY_ACTION, payload: {foo: 'bar'}}start:myAction.start,// start({foo: 'bar'}) -> {type: MY_ACTION_STARTED, payload: {foo: 'bar'}}fetch:myAction.fetch,// fetch({foo: 'bar'}) -> {type: MY_ACTION_FETCH, payload: {foo: 'bar'}}update:myAction.update,// update({foo: 'bar'}) -> {type: MY_ACTION_UPDATE, payload: {foo: 'bar'}}create:myAction.create,// create({foo: 'bar'}) -> {type: MY_ACTION_CREATE, payload: {foo: 'bar'}}remove:myAction.remove,// remove({foo: 'bar'}) -> {type: MY_ACTION_REMOVE, payload: {foo: 'bar'}}resolve:myAction.resolve,// resolve({foo: 'bar'}) -> {type: MY_ACTION_RESOLVED, payload: {foo: 'bar'}}reject:myAction.reject// reject({foo: 'bar'}) -> {type: MY_ACTION_REJECTED, payload: {foo: 'bar'}}}// Getting action status, error and response// Using this with this `myAction.resolve({foo: 'bar'})` as the last action dispatchedconstmapStateToProps=state=>({status:myAction.getStatus(state),// => 'resolved'error:myAction.getError(state),// => undefinedresponse:myAction.getResponse(state)// => {foo: 'bar'})}// Using actions in reducersswitch(type){casemyAction.TYPE:// => 'MY_ACTION'return{            ...state,hello: 'me'}casemyAction.STARTED:// => 'MY_ACTION_STARTED'return{            ...state,hello:'cat'}casemyAction.FETCH:// => 'MY_ACTION_FETCH'return{            ...state,hello:'rat'}casemyAction.UPDATE:// => 'MY_ACTION_UPDATE'return{            ...state,hello:'rabbit'}casemyAction.CREATE:// => 'MY_ACTION_CREATE'return{            ...state,hello:'owl'}casemyAction.REMOVE:// => 'MY_ACTION_REMOVE'return{            ...state,hello:'elephant'}casemyAction.RESOLVED:// => 'MY_ACTION_RESOLVED'return{            ...state,hello:'dog'}casemyAction.REJECTED:// => 'MY_ACTION_REJECTED'return{            ...state,hello:'dodo'}default:returnstate}

Why

Sometimes you need an action status and you had to create a new reducer just for that, also, this reduces the boilerplate of creating a lot of actions for all your async actions.

Installation

yarn add async-action-creator// ornpm install --save async-action-creator

Tests

// jest testsyarntest// jest coverageyarn cover

Contributors

Simply create a pull request :)

  • Code style:Standard
  • FlowType used

Thanks

This project is based on the idea ofmake-action-creator by@ajchambeaud, and thegetStatus andgetError implementations to the same library from@pablen.

License

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, 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 INTHE SOFTWARE.

About

Action creator with steroids for Redux or whatever you want

Resources

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp