Coder Social home page Coder Social logo

Comments (2)

archywillhe avatar archywillhe commented on June 11, 2024 1

:(

from redux-actions.

simondell avatar simondell commented on June 11, 2024

Flux Standard Actions, which redux-actions providers helpers for, represent success and error "versions" of an action as the same action type. createAction() will create action-creator functions which accept either a value or an error object. When the code which may have success or error paths needs to dispatch an action, you can use the same action creator.

// action creators.js
const readDataCompleted = createAction('DATA/READ_COMPLETED`)

// getdata.js
import somethingHappened from './actions'
import store from './store'

try {
  const response = await fetch('some_url')
  
  if(!response.ok) throw new Error('Bad times on the internetz.')

  const body = await response.json()
  store.dispatch(somethingHappened(body))
}
catch (err) {
  store.dispatch(somethingHappened(err))
}

Code similar to this will produce a "success" action when following the happy path:

{
  type: 'DATA/READ_COMPLETED',
  payload: /* ... your data ... */
}

... and an 'error' action when following the error path:

{
  type: 'DATA/READ_COMPLETED',
  error: { message: 'Bad times on the internetz.' }
}
// well, it'll be a proper error object, not the pseudo-error object I wrote above.

This means that your reducers need to contain logic for handling the success and error states. You can roll your own, or use handleAction()/handleActions(), also included in the redux-actions library, to help create these "error aware" reducers. I'll leave you to look at the docs for handleAction().

from redux-actions.

Related Issues (20)

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.