Comments (12)
@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.
@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.
@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.
@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.
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.
@mironov Thank you. I will try it out.
from react-redux-loading-bar.
@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.
@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.
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.
@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.
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.
@mironov
Thanks for the tip and it's working now.
from react-redux-loading-bar.
Related Issues (20)
- Minified react error 321 HOT 1
- Can't get loading bar to stay within parent container HOT 2
- Loading bar is not displayed? HOT 6
- Financing HOT 2
- When array data in response have more than one item - hiding is skipping in production HOT 1
- Update peerDependencies to include React 18 HOT 1
- CreateSlice Redux HOT 1
- Error: Element type is invalid HOT 1
- [Question] Does this support the [email protected]? HOT 4
- How to use with react-router HOT 2
- It does not render any classname nor interact with the store HOT 2
- Step Progress HOT 1
- RTL Support HOT 1
- Usage without redux HOT 3
- Highlight updates shows up a blinking loading bar. HOT 2
- Facing following issue while using 'react-redux-loading-bar' HOT 1
- `resetLoading()` method is not fully resetting the component. HOT 3
- Loading bar not working with redux-persist HOT 3
- × Could not find "store" in the context of "Connect(LoadingBar)". HOT 3
- Use for polling HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-redux-loading-bar.