Coder Social home page Coder Social logo

Comments (12)

mironov avatar mironov commented on May 27, 2024 2

@nextofsearch It seems that you don't use redux-promise-middleware and thus can't rely on its action type names.

Do you use fetchData across the app whenever you need to fetch any data? If yes, you can simply put showLoading and hideLoading calls in it.
You can dispatch showLoading before dispatching beforeActionCreator and hideLoading before successActionCreator and failureActionCreator .

I'm going to close the issue because that is not an issue with the lib. Feel free to use the issue to discuss the usage of the lib with redux-thunk.

from react-redux-loading-bar.

mironov avatar mironov commented on May 27, 2024 1

@nextofsearch I've been using redux-promise-middleware for quite a while. It's great and works out-of-the-box with react-redux-loading-bar.

from react-redux-loading-bar.

mironov avatar mironov commented on May 27, 2024 1

@jamby Sorry for the late reply.
I created a branch of demo with axios. You can take a look at the diff here:
gh-pages...demo-axios

from react-redux-loading-bar.

mironov avatar mironov commented on May 27, 2024

@nextofsearch Hi.

In order to use react-redux-loading-bar with redux-thunk you'll need to dispatch showLoading and hideLoading actions from within your actions.

Your actions should look like that:

import { showLoading, hideLoading } from 'react-redux-loading-bar'

function incrementAsync() {
  return dispatch => {
    dispatch(showLoading())
    setTimeout(() => {
      dispatch(increment());
      dispatch(hideLoading())
    }, 1000);
  };
}

from react-redux-loading-bar.

nextofsearch avatar nextofsearch commented on May 27, 2024

Hi @mironov

Oh, I thought redux-thunk is one of middleware using Promises middleware. I just want to avoid adding showLoading() & hideLoading() to every action. What would be your recommendation of easy-to-use middleware choice?

Thanks!

from react-redux-loading-bar.

nextofsearch avatar nextofsearch commented on May 27, 2024

@mironov Thank you. I will try it out.

from react-redux-loading-bar.

jamby avatar jamby commented on May 27, 2024

@nextofsearch Did you figure out a way to get it to work with redux-thunk and redux-promise-middleware? I also do not want to call dispatch(showLoading()) and dispatch(hideLoading()) every time. I would rather any AJAX request turns it on.

from react-redux-loading-bar.

mironov avatar mironov commented on May 27, 2024

@jamby How do you perform AJAX requests? If you use redux-promise-middleware and fetch, then react-redux-loading-bar will work out of the box. You'll need to install the loadingBarMiddleware as it is mentioned in Readme.

from react-redux-loading-bar.

jamby avatar jamby commented on May 27, 2024

Currently using redux-thunk and then doing an axios request for posting/getting information.

For instance:

return (dispatch) => {
    axios({
      method: 'get',
      url: '/api/data',
    }).then(({ data }) => {
      dispatch(setData(data));
    }).catch((error) => {
      console.log(error.response);
    });
  };

from react-redux-loading-bar.

nextofsearch avatar nextofsearch commented on May 27, 2024

@jamby I haven't figured it out yet.
@mironov I am using thunk and 'isomorphic-fetch'. Is there any workaround for me as you suggested in the demo link above.

My custom fetching code is:

import fetch from 'isomorphic-fetch'

export function fetchData (url, beforeActionCreator, successActionCreator, failureActionCreator, options = {}) {

  return dispatch => {
    if(beforeActionCreator) {
      dispatch(beforeActionCreator())
    }

    return (
      fetch(url, requestOptions).
      then(checkStatus).
      then(parseJSON).
      then(json => dispatch(successActionCreator(json))).
      then(() => {
        if(typeof(successCallback) === 'function') {
           successCallback()
        }
      }).
      catch(error => {
        console.log('error', error)
        let promise = error.response.json().
                                     then(json => dispatch(failureActionCreator(json.message)))

        if(typeof(failureCallback) === 'function') {
          failureCallback(error)
        }

        return promise
      })
    )
  }

from react-redux-loading-bar.

jamby avatar jamby commented on May 27, 2024

At first I was confused on how it works especially for chaining dispatches, but then I looked at the complex redux-promise-middleware demo and saw how they did it.

https://github.com/pburtchaell/redux-promise-middleware/tree/master/examples/complex

Thanks!

from react-redux-loading-bar.

nextofsearch avatar nextofsearch commented on May 27, 2024

@mironov
Thanks for the tip and it's working now.

from react-redux-loading-bar.

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.