Giter VIP home page Giter VIP logo

Comments (5)

addnab avatar addnab commented on June 21, 2024 30

redux-thunk is a middleware unlike redux-actions which is a utility library for creating and handling actions. So you can still continue to use redux-thunk to dispatch multiple actions and use redux-actions API to createAction and handleActions like:

action.js

const fetchRequest = createAction('FETCH_REQUEST')
const fetchSuccess = createAction('FETCH_SUCCESS')
const fetchFailure = createAction('FETCH_FAILURE)
const fetch = (url) => {
    return async (dispatch, getState) => {
        try {
            dispatch(fetchRequest())
            const response= await _fetch(url)
            if(result.statusCode !== 200)
                throw new Error()
            const result = await response.json()
            dispatch(fetchSuccess(result))
        }
        catch(error) {
            dispatch(fetchFailure(error))
        }
    }
}

reducers.js

const reducer = handleActions({
  FETCH_REQUEST: (state, action) => ({
    ...state,
    fetchError: false,
    isFetching: true,
    displayText: "Fetching..."
  },

  FETCH_SUCCESS: (state, action) => ({
    ...state,
    fetchError: false,
    isFetching: false,
    displayText: action.payload
  }),

  FETCH_FAILURE: (state, action) => ({
    ...state,
    fetchError: true,
    isFetching: false,
    displayText: action.payload 
  })
});

from redux-actions.

Industrial avatar Industrial commented on June 21, 2024

Okay,

so then in my components where do I pass dispatch? or is the redux-thunk library still doing that?

function mapDispatchToProps(dispatch) {
  return {
    fetch: (url) => dispatch(actions.fetch(url))
  };
}

from redux-actions.

addnab avatar addnab commented on June 21, 2024

@Industrial Yes, you don't have to pass dispatch while dispatching the action. If the action returns a function of the form foo(dispatch, getState), then redux-thunk middleware should take care of calling that function by passing dispatch and getState as arguments to that function.

See here.

from redux-actions.

timche avatar timche commented on June 21, 2024

Seems like the question has been answered.

from redux-actions.

moersing avatar moersing commented on June 21, 2024

@addnab Thank you , This is good solution.

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.