I am beginner of react application. I need handle asynchronous call in my application.
For the implement asynchronous call is there any library for that ?
How to implement asynchronous call using library ?
For the implement asynchronous call you can use following library with react-redux
You can learn asynchronous call handling using library documentation
If you are using vanilla ReactJS and your App does not need any Flux or Redux to control the state of the App, you can use React life-cycle method componentWillMount for that with builtin fetch method or library like axios
If your Application needs Flux or Redux then I personally recommend Redux over Flux and you can use redux-thunk middleware for that. for example
const INCREMENT_COUNTER = 'INCREMENT_COUNTER';
function increment() {
return {
type: INCREMENT_COUNTER
};
}
function incrementAsync() {
return dispatch => {
setTimeout(() => {
// Yay! Can invoke sync or async actions with `dispatch`
dispatch(increment());
}, 1000);
};
}
For more see http://redux.js.org/docs/advanced/AsyncActions.html
Here is the documentation for how to use async actions : http://redux.js.org/docs/advanced/AsyncActions.html
You define it like so when configuring the store:
const store = createStore(
rootReducer,
applyMiddleware(
thunkMiddleware
)
)
And you use it in the actions like so:
function fetchAction(data) {
// Return a function, this function will be activated the the thunk middleware with dispatch as parameter
return function (dispatch) {
// Dispatch some action before fetch (like setting loading)
dispatch(requestData(data));
// return a promise of the fetch
return fetch('yoururl')
.then(response => response.json())
.then(json =>
// dispatch a new action when you get your data
dispatch(receivedData(data,json))
).catch (e = >
// you can also dispatch an error
dispatch(errorData(data,e));
)
}
}