Comments (5)
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.
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.
@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.
Seems like the question has been answered.
from redux-actions.
@addnab Thank you , This is good solution.
from redux-actions.
Related Issues (20)
- [Feature request] Function value for defaultState HOT 1
- it's support immutable.js?
- can't use Map on handleActions HOT 2
- combine-actions prefixing bug
- Error handling on action payload HOT 2
- Docs: broken link
- Suddenly can't add an action... HOT 3
- How to use react hooks aftrer reducer change for the lib?
- Does state changing is async? HOT 3
- Looking for Maintainers
- React SSR handleActions cache bug HOT 1
- handleActions with Immer. HOT 1
- Wierd behavior with just not recognizing string templated reducer HOT 1
- 'to-camel-case' pacakge introducing bug since it's not compatible with 'lodash.camelCase' HOT 1
- v3.0.0 + TypeScript Cannot find module 'redux-actions' from ... HOT 12
- Import fails to identify entry point HOT 2
- can redux-actions be used with redux-thunk?
- Not Working with React Native from v.3.0.0
- Compilation error in `react-native:0.71.8` HOT 1
- Not working with `Jest` from v3.0.0 HOT 1
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 redux-actions.